Jump to content
  • Advertisement
Sign in to follow this  
CRACK123

Unnecessary character /377 while loading file in C

This topic is 4581 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, When I load a text file in C I seem to be getting an extra character \377. I am not sure what this is, but it is not there when I actually view the file. But while loading I seem to get it from nowhere. Unfortunately I can't post the code if its a code problem. But I have debugged and I can't figure out where the problem lies. In Solaris the code seems to work fine and shows me the \377 but in Visual Studio it crashes and on debugging I see a wierd symbol. Also at that point pointer points to some junk value - something like 0xfeeefeee. Any ideas why this might be happening ? Thanks

Share this post


Link to post
Share on other sites
Advertisement
That is a decimal 255, which is -1 for a signed byte (the typical char type). -1 is the value returned from the read functions on end-of-file.

Share this post


Link to post
Share on other sites
It apparently seems to be reading that in as well. My code goes something like the following


while(!feof(fp))
{
ch = fgetc(fp));

......
}




It seems like fgetc reads in the end of file as well.

Any ideas on how to get around this ?

Share this post


Link to post
Share on other sites
Quote:
Original post by CRACK123
It apparently seems to be reading that in as well. My code goes something like the following

*** Source Snippet Removed ***

It seems like fgetc reads in the end of file as well.

Any ideas on how to get around this ?


Well, you could try something like:

do {
ch = fgetc(fp);
// ...
} while (ch != 0377);

Share this post


Link to post
Share on other sites
eof isn't set until you actually "read" the end-of-file. Try this:


while ((ch = fgetc(fp)) != EOF)
{
// do whatever
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Roboguy

Well, you could try something like:

do {
ch = fgetc(fp);
// ...
} while (ch != 0377);


That won't work, because ch would still be -1 inside the loop.

Share this post


Link to post
Share on other sites
Quote:
Original post by CRACK123
Any ideas on how to get around this ?


while(true)
{
ch = fread(fp);
if feof(fp) break;

...
};

Share this post


Link to post
Share on other sites
Thanks for the help guys.

Apparently fgetc wasn't the best option as I had imagined. I removed the fgetc and used fread to read in the file at one go.


Share this post


Link to post
Share on other sites
Just so it's 100% clear what is going on here...

fgetc is not "reading in the end of file". It's is returning an error, which you did not check for and thus passed garbage to the rest of your logic. It's not really any different than if you had done something like:

int * p = malloc(some_really_really_big_amount_of_memory);
*p = 0; // crash because p is NULL and wasn't checked

Share this post


Link to post
Share on other sites
while ((ch = fgetc(fp)) != EOF)
{
// do whatever
}

Likely the best solution there from Dave. Read the docs on the command you're using and you'll understand the issue and maybe how to fix it. I prefer MSDN as I find it's clear but even the man pages are great (sometimes I swear its word for word). Pick which you like best and abuse them :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!