Thread: feof issues

  1. #1
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214

    feof issues

    well to start off...
    a college course i am taking has a C module on it and we just had our finnal test using file i/o, i done it one way and the lecturer said its bad and i lost 10 marks..now he seems not to bright and im really sure my way is more efficiant..here is how it went

    Code:
    //my way
    while(!feof(fp))
    {
    	fscanf(fp , "%d %f %d" , &amount , &cost, &rate);
    	i++;
    }
    Code:
    //lecturers way
    fscanf(fp , "%d %f %d" , &amount , &cost, &rate);
    while(!feof(fp))
    {
    	fscanf(fp , "%d %f %d" , &amount , &cost, &rate);
    	i++;
    }
    he has told me my way will crash the program if the file is empty (he seems to think u need to read from the file first before u can use feof on it) and he said it will produce garbage in the arrays

    can some one please give me some opinions on this and some ways to try and get him to give me 10 points back, thanks
    Good Help Source all round
    Good help for win programmers
    you will probably find something in here
    this thing also helps

    if you have never tried any of the above then maybe you should, they help alot

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You might want to read up on this entry in the FAQ.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well you could read this
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    Your lecturer is correct, though his solution is a little on the clunky side
    Code:
    while ( fscanf(fp , "%d %f %d" , &amount , &cost, &rate) == 3 ) {
      i++;
    }
    See - only one fscanf() call to get right, rather than two.
    It also stops if the file becomes garbage as well as at end of file.

  4. #4
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    thanks, but wat really is the difference between mine and his..other than the fact his will read in null if the file is empty which can cause problems, and why did he take off 10 marks?
    Good Help Source all round
    Good help for win programmers
    you will probably find something in here
    this thing also helps

    if you have never tried any of the above then maybe you should, they help alot

  5. #5
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    oh..and i should have mentioned, we had to use feof
    Good Help Source all round
    Good help for win programmers
    you will probably find something in here
    this thing also helps

    if you have never tried any of the above then maybe you should, they help alot

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well I suggest you try yours, his and mine with an empty file
    - see how many times the loop is executed
    - see what values you get printed

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > we had to use feof
    Presumably to demonstrate that its use in this context is broken?

  8. #8
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    i wouldnt have a clue why,
    i have tried to crash both and failed to do so..
    i cant really figure out why i lost 10 marks when mine seems more safe and does the same thing?
    Good Help Source all round
    Good help for win programmers
    you will probably find something in here
    this thing also helps

    if you have never tried any of the above then maybe you should, they help alot

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > i have tried to crash both and failed to do so..
    Well it probably won't crash
    But junk data is pretty much guaranteed.

    See my fscanf call - check the return result (which neither you nor your prof did)

  10. #10
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    it returned -1
    Good Help Source all round
    Good help for win programmers
    you will probably find something in here
    this thing also helps

    if you have never tried any of the above then maybe you should, they help alot

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Which is EOF

    So if you had written
    Code:
    while(!feof(fp))
    {
    	if ( fscanf(fp , "%d %f %d" , &amount , &cost, &rate) ) != EOF ) {
    		i++;
    	}
    }
    You'd be better off.

    But still not as good as mine

  12. #12
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    well the thing is..its allready done and i need to find a way to convince him mine is fine and his is not as hot as he thinks,
    he thinks u need to read from the file first to set the posistion i think
    Good Help Source all round
    Good help for win programmers
    you will probably find something in here
    this thing also helps

    if you have never tried any of the above then maybe you should, they help alot

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, considering that feof will not signal end of file until an attempt has been made to read past the EOF marker, his is "more correct" than yours, in that the first call to feof in yours won't really do anything. So technically, his is better, because it gives feof a chance to actually catch something on the first pass.

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > but wat really is the difference between mine and his
    Code:
    FILE *fp = fopen( "empty.txt", "r" );
    if ( feof( fp ) ) {
      printf( "you're right\n" );
    } else {
      printf( "you're wrong\n" );
    }
    But you're both wrong in my book

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. issues in float division a/b
    By George2 in forum C# Programming
    Replies: 17
    Last Post: 04-24-2008, 06:15 AM
  2. Multi-platform support issues.
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-27-2004, 06:41 PM
  3. hexdump issues
    By daluu in forum C Programming
    Replies: 2
    Last Post: 03-04-2003, 09:01 PM
  4. feof ....EOF
    By GanglyLamb in forum C Programming
    Replies: 12
    Last Post: 12-04-2002, 12:38 PM
  5. advice on feof()
    By Hobo in forum C Programming
    Replies: 2
    Last Post: 09-07-2002, 08:15 AM