Thread: Quick question on reading files in C

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    103

    Quick question on reading files in C

    I don't know how to tell the computer to stop reading when the file is done. I was looking around online and one example had
    Code:
    while(!feof(file))
    but don't that gave me a seg fault. Can anyone clarify this for me?

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    actually it reads into some space and returns weird numbers.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    looking at this example confused me, could you explain why they did an fgets on buffer?

    Code:
    while (fgets(buf, sizeof(buf), fp) != NULL)

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because that's how you read things in. fgets is, I mean.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    no i understand fgets but I don't understand why they pass in buf and sizeOf(buf). I don't get how that has anything to do with the file size of junk1.txt

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It doesn't. It's not supposed to.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The idea, TaiL, is that:

    "If the system is already at end of file when you call fgets, then the contents of the array are unchanged and a null pointer is returned." (GNU)

    There could be a flaw in this method too if the EOF occurs before/without a newline.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    so is this basically the concept?

    char nullChkr[50];

    while (fgets(nullChkr, 50, fp) != NULL)

    nullChkr would get overwritten with a NULL when fp is done. thus kicking me out of the loop?

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    No, nullchkr wouldn't get overwritten -- fgets would return NULL and not change the value of the argument.

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by TaiL View Post
    nullChkr would get overwritten with a NULL when fp is done. thus kicking me out of the loop?
    Don't think about it as "nullChkr", though (it's a filled character buffer) -- if you just want to find the last byte of a file , use a ++loop with fseek. If you decide to do that then take data from the file, you need to rewind the filestream.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    No. nullChkr will not be overwritten with NULL. nullChkr gets filled with bytes read from the file. If there is an EOF detected, you may not get the full 49 (+1 for the nul) bytes asked for... The string, however much of it, in nullChkr will be terminated by a nul in any case. fgets() returns the pointer to the string, or NULL - in which case nullChkr's contents would not be touched.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Question on Multiple Source Files
    By dac in forum C++ Programming
    Replies: 24
    Last Post: 12-01-2006, 03:53 PM
  2. Question about binary trees and files
    By satory in forum C Programming
    Replies: 9
    Last Post: 03-06-2006, 06:28 AM
  3. Question about IOStream and reading strings from files
    By kingpinzs in forum C++ Programming
    Replies: 22
    Last Post: 12-13-2005, 11:29 AM
  4. Question about ".o" files
    By Jez_Master in forum C++ Programming
    Replies: 1
    Last Post: 04-11-2002, 01:54 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM