×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Problem with EOF?

Problem with EOF?

Problem with EOF?

(OP)
I have written this routine for assigning the bytes of a (large) file to
an int type array and the loop is supposed to end at the end of file.
Unfortunately it exits the loop ( EOF is reached ), a lot more earlier
than the actual end of file! Here is the code of the routine:

/******************************************************************************

This function accepts a file and returns through the function the file
size and through an array the bytes of the file.

******************************************************************************/

int assign(FILE *fp, int bytes[])
{
   int c, index = 0;

   while ((c=getc(fp)) != EOF)
    {
       bytes[index] = c;
       index++;
    }  
return index;
}


Any ideas why?

RE: Problem with EOF?

Well... If u said that the file is indeed very large... You should know that the int type ranges from -32768 or so to 32768 or so...
If your file is larger that 32k, your counter will reach the highest value (32768) and then, it will start again from -32768 (because the int type is stored on 16 bits, the msb its the bit sign). This should mean that you actually read all the file, but it is not stored as you would expect.

I'm still not sure that this is the problem. you should trace the value of index, because as far as i know,when indexing an array with negative numbers, the program should crash.

Anyway, if this is the problem, change the index type to long and you've eradicated the problem.

Hope i helped,
Nosferatu.

RE: Problem with EOF?

@Aphrodita:
Yes it might take some time, but this what debugging it is all about, isnt' it?

RE: Problem with EOF?

Theodotos,

Your code doesn't show us how you are opening the file. Is it possible that you are opening a binary file in text mode?
Just a thought.
Also, what value does your routine return to the calling program? As previous posts point out, you could be overflowing the int variable index. You should pass the sizeof bytes[]to your routine thus avoiding overflows and writing outside the allocated memory of the array.
 

 
Kim_Christensen@telus.net
http://www3.bc.sympatico.ca/Kim_Christensen/index.html
 

RE: Problem with EOF?

zBuilder has correctly pointed that you are in fact trying to read a binary file in text mode. A binary file can contain all kinds of values, just not the ASCII values that a text file contains. So, a binary file can contain the value '-1' anywhere in it, not just at the end of a file. This value: -1 is EOF only in a text file, because there is no ASCII character corresponding to this value, so the program can know that it has come to the end of a file when it sees this value. For knowing the end of a binary file, use the function: feof() instead. Remember, this function returns a true when an end is reached, so for looping, you would negate its return value, that is, use if(!feof()) {...}

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close