Thread: File input

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    24

    File input

    I have been using fgets() to read lines of a file I have and store them in a linked list. This has been working very well but after the last line I seem to end up with "x(smiley face)>" in the list. I have checked and this string does indeed result from the last call to fgets before EOF. I have used a hex editor to check the file and at the end of the last line is just the usual 0D 0A. What is going on here?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Golf7 View Post
    I have been using fgets() to read lines of a file I have and store them in a linked list. This has been working very well but after the last line I seem to end up with "x(smiley face)>" in the list. I have checked and this string does indeed result from the last call to fgets before EOF. I have used a hex editor to check the file and at the end of the last line is just the usual 0D 0A. What is going on here?
    Right there. See that spot in your code? That's what you did wrong.


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

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    It sounds like you're saving the data from your failed read. Remember that EOF isn't set until reading fails. The last bit of data read must be discarded.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    24
    Here is the revevant part of the code:
    Code:
    int main(void) {
    	FILE *filein;
    	filein = fopen("input.txt", "r");
    	struct Node* head = NULL;
    	char* input;
    	while(!feof(filein)) {
    		input = malloc(256);
    		fgets(input, 256, filein);
    		ListAdd(&head, input);
    	}
    	ListPrint(head);
    	ListCleanup(head);
            fclose(filein);
    	return 0;
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Read the FAQ on why it is bad to use feof() to control a loop (in this way).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Read: Cprogramming.com FAQ > Why it's bad to use feof() to control a loop

    Code:
    char buf[ BUFSIZ ] = {0};
    while( fgets( buf, BUFSIZ, filein ) != NULL )
    {
        ... malloc + strcpy + add to list
    }
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input File to Output File
    By ejiroy in forum C++ Programming
    Replies: 1
    Last Post: 04-26-2009, 11:40 PM
  2. linking file stream with input file?
    By flamehead144 in forum C Programming
    Replies: 8
    Last Post: 02-07-2009, 08:55 PM
  3. file input and file pointer problem
    By drater in forum C Programming
    Replies: 6
    Last Post: 02-18-2008, 10:43 AM
  4. Input-Output File--Can't create a file...
    By zaracattle in forum C++ Programming
    Replies: 10
    Last Post: 10-18-2006, 10:15 AM
  5. File I/O with 2 input and 1 output file
    By Sandy in forum C Programming
    Replies: 1
    Last Post: 04-19-2003, 12:06 PM