Thread: fgets misses first character in file :?

  1. #1
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154

    fgets misses first character in file :?

    Hi guys,

    I'm having problems with reading the first line in a certain text file. I use the following function to do this:
    Code:
       fpin = fopen(id_index, "rb");
       while((ih=fgetc(fpin))!=EOF)
       {
          while(fgets(buf,BUFSIZ,fpin)!=NULL)
          {
             strcpy(id_array[cnt],buf);
             cnt++; // count the amount of identifier files found
          }
       }
       fclose(fpin);
    This is what the text file (id_index) looks like:
    840.txt
    1224.txt
    1293.txt
    520.txt
    909.txt
    1101.txt
    1042.txt
    1160.txt
    1294.txt
    1928.txt
    1938.txt
    1933.txt
    2018.txt
    1900.txt
    1899.txt
    Now when I save those names, line by line into an array, the first value of the array is:
    id_array[0]: 40.txt
    So it misses the first character! Any idea how I can solve this problem?

    Thanks in advance for any help!


    René
    Last edited by rkooij; 04-26-2006 at 07:41 AM.
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you read the first char with fgetc
    then the rest of the line with fgets

    > while((ih=fgetc(fpin))!=EOF)
    This is on the whole pretty pointless
    The first time, it burns a char
    The second time, it returns EOF, because the inner loop has read all the way to end of file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Quote Originally Posted by Salem
    Well you read the first char with fgetc
    then the rest of the line with fgets

    > while((ih=fgetc(fpin))!=EOF)
    This is on the whole pretty pointless
    The first time, it burns a char
    The second time, it returns EOF, because the inner loop has read all the way to end of file.
    Ha, you're 100% right! I have copied and misinterpreted that part of the function from another function. I did always question the exact meaning of this line but never bothered to dive into it . Now that you've explained it's as obvious as possible!
    Thanks very much!
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM