Thread: feof question

  1. #1
    Registered User
    Join Date
    Apr 2009

    feof question

    so i have a while(!feof) loop set up in my program but i keep getting seg faults because for some reason my program keeps scaning (im scaning with an fscanf btw) off the end of the file. . . i tried scaning 2 or 3 of the data and it runs perfectly, i just cant get my program to stop scanning. . . any suggestions?

    EDIT: like is there a way to put an EOF character at the end of my text file im scanning from?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    Sounds unlikely - I have never in my life seen a segfault due to reading past end of file.

    Having said that, you should check the input for EOF since feof() only returns true AFTER you have failed to read from the file. So for example this:
    FILE *input;
    char *ptr;
    char buffer[200];
    input = fopen("text.txt", "r");
        ptr = fgets(buffer, sizeof(buffer), input);
        if (strlen(ptr) > 5)
          printf("%s\n", ptr);
    Most modern OS's do not "use" a EOF character in the file, but rather have an exact count of how many bytes is in the file- older file-systems that would only track "blocks" needed to put a EOF character at the end of the file to show where the last block ended, as otherwise you may get 127 characters worth of rubbish in a 128 byte block, or some such.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    First, you could post a bit of code.

    Second, avoid using feof to control a loop it usually just results in problems unless done correctly. Typically, you want to place the read operation (fscanf in your case) in the loop's conditional part instead of the feof call. fscanf returns the number of arguments it was able to successfully convert. If this value is what it should be then you've successfully read a line/record from the file. If not, then you've either got bad data or you're at the end of the file:

    while( fscanf(...) == however_many_values_you_expect )
        /* Do stuff */
    If your file's data is arranged one record per line, most people would use fgets combined with sscanf to parse the files data:
    while( fgets(...) != NULL )
        sscanf(...);  /* Parse the retrieved data */
        /* Do stuff */
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM