Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with EOF?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
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?
 
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.
 
@Aphrodita:
Yes it might take some time, but this what debugging it is all about, isnt' it?
 
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
 
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()) {...}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top