Like Tree1Likes
  • 1 Post By quzah

File input

This is a discussion on File input within the C Programming forums, part of the General Programming Boards category; I have been using fgets() to read lines of a file I have and store them in a linked list. ...

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

    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.
    AndrewHunter likes this.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023
    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
    14
    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
    21,598
    Read the FAQ on why it is bad to use feof() to control a loop (in this way).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 07:55 PM
  3. file input and file pointer problem
    By drater in forum C Programming
    Replies: 6
    Last Post: 02-18-2008, 09: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21