Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 reading text / windows XP error
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Tubs




PostPosted: 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 Crying or Very sad

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
Sponsor
Sponsor
Sponsor
sponsor
Monstrosity_




PostPosted: 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
Tubs




PostPosted: Thu Nov 03, 2005 1:29 pm   Post subject: (No subject)

uhhhhh what.
Monstrosity_




PostPosted: 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.
beard0




PostPosted: 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.
Monstrosity_




PostPosted: 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.
Tubs




PostPosted: 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?
wtd




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: