Thread: File pointer lists file twice?

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    5

    Question File pointer lists file twice?

    Hey,

    Thanks ahead of time for any help you can give me on a problem Im having.


    I have a UNIX C program that assigns a text list to a file pointer name "DirListPtr". Every time I run the program to go through the file, it lists the last entry in the text list twice.

    I am stumped, any ideas?

    The program uses the statement to go through the file.

    Code:
    while ((feof(DirListPtr)) == 0)


    Thanks again,

    Willz99ta

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    5

    Cool Thanks

    I dont think I would ever figured that out...Thanks!


    Willz99ta

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    basically what the tutorial said is that when you read from the file, do not use feof() to check for the end-of-file. Instead use fgets() to fetch from the file, line by line and check the return value of fgets(). fgets() will return null on reading the end-of-file.

    so you file read loop would be

    Code:
    char line[100];
    
    while ((fgets(line,sizeof(line),DirListPtr)) != NULL)
    {
        /*Process "line"*/
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Problems with file pointer in functions
    By willie in forum C Programming
    Replies: 6
    Last Post: 11-23-2008, 01:54 PM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  5. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM