reading text / windows XP error
Author |
Message |
Tubs
![](http://members.lycos.co.uk/lovedtwice2000/phpBB2/images/avatars/187282473e3fd65a3a33b.gif)
|
Posted: Wed Nov 02, 2005 12:30 pm Post subject: reading text / windows XP error |
|
|
i'm aware that when compiling a program that uses file reading / writing windows XP doesnt allow it to run, but when the .exe file is ran it is supposed to work properly (ive done it before). but this code still crashes when ran with the .exe and i dont know why
code: | #include <stdio.h>
int main(int argc, char *argv[])
{
int hot = 0, pleasant = 0, cold = 0, total = 0, count = 0, temp;
FILE *in, *out;
in = fopen( "data.txt", "r" ); // open a file for reading
while( fscanf( in, "%d", &temp ) != EOF ) // Read from the file until EOF
{
total += temp;
count += 1;
if (temp >= 85)
{
printf ("Temperature: %d is hot.\n", temp);
hot += 1;
}
else if (temp >= 60 && temp < 85)
{
printf ("Temperature: %d is pleasant.\n", temp);
pleasant += 1;
}
else if (temp < 60)
{
printf ("Temperature: %d is cold.\n", temp);
cold += 1;
}
}
fclose( in );
printf ("\n\nThere were %d hot days, %d pleasant days and %d cold days.", hot, pleasant, cold);
printf ("The average temperature was %.2f", total / count);
system ("pause");
return 0;
} |
dev c ++ compiler |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Monstrosity_
|
Posted: Wed Nov 02, 2005 7:30 pm Post subject: Re: reading text / windows XP error |
|
|
Tubs wrote: ...
FILE *in, *out;
in = fopen( "data.txt", "r" ); // open a file for reading
while( fscanf( in, "%d", &temp ) != EOF ) // Read from the file until EOF
...
Your not checking what fopen returns |
|
|
|
|
![](images/spacer.gif) |
Tubs
![](http://members.lycos.co.uk/lovedtwice2000/phpBB2/images/avatars/187282473e3fd65a3a33b.gif)
|
Posted: Thu Nov 03, 2005 1:29 pm Post subject: (No subject) |
|
|
uhhhhh what. |
|
|
|
|
![](images/spacer.gif) |
Monstrosity_
|
Posted: Thu Nov 03, 2005 1:46 pm Post subject: Re: reading text / windows XP error |
|
|
When you call a function, it is usually good to know what its return values are. For fopen, as your man pages will tell you, it will return a FILE pointer if successful, and NULL otherwise. And you neglect to check what fopen returns and open your program up to undefined behaviour when it fails.
So, the problem is your not checking what fopen returns. |
|
|
|
|
![](images/spacer.gif) |
beard0
![](http://ugray.be/compsci/silly.gif)
|
Posted: Thu Nov 03, 2005 3:39 pm Post subject: Re: reading text / windows XP error |
|
|
Monstrosity_ wrote: So, the problem is your not checking what fopen returns.
No, the problem is that fopen is returning NULL. All that checking the return value would accomplish is to have the program finish unsuccessfully rather than crashing - it would still be a problem. As to how to fix your problem, sorry but I don't know. I was just bugged by this point. |
|
|
|
|
![](images/spacer.gif) |
Monstrosity_
|
Posted: Thu Nov 03, 2005 3:56 pm Post subject: Re: reading text / windows XP error |
|
|
beard0 wrote: No, the problem is that fopen is returning NULL. All that checking the return value would accomplish is to have the program finish unsuccessfully rather than crashing - it would still be a problem.
Not quite, by checking the return value and reading the man page he can find out exactly why it failed. And I also find undefined behaviour a more serious problem then not being able to open a file.
Edit: I do see where you are comming from though, as I forgot to mention that it will give you more information when it returns NULL. |
|
|
|
|
![](images/spacer.gif) |
Tubs
![](http://members.lycos.co.uk/lovedtwice2000/phpBB2/images/avatars/187282473e3fd65a3a33b.gif)
|
Posted: Thu Nov 03, 2005 7:16 pm Post subject: (No subject) |
|
|
the problem wasnt in the program.
in windows, there is an option that hides extentions on files. it turns out i renamed the text file i was using to 'data.txt' and in actuality it was 'data.txt.txt'. stupidest windows feature, ever? |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Thu Nov 03, 2005 7:21 pm Post subject: (No subject) |
|
|
The problem is in your program. There's always the possibility that the file you're tellng it to open doesn't exist. Your program isn't robust, and doesn't check for that problem. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
|
|