cant read last char from a file

This is a discussion on cant read last char from a file within the C Programming forums, part of the General Programming Boards category; hey guys! i make a code to open and read a text file. Code: while( fgets(tmpstr, 127, fp) ){ it ...

  1. #1
    aze
    aze is offline
    Registered User
    Join Date
    Sep 2003
    Posts
    18

    cant read last char from a file

    hey guys!

    i make a code to open and read a text file.
    Code:
    		 while( fgets(tmpstr, 127, fp) ){
    it runs ok. but when the file dont have a \n char being the last char in the file, my code cant read the last file's char.

    file1---------------------
    text 123
    just junk text
    more text

    end file 1---------------

    file2---------------------
    text 123
    just junk text
    more text
    end file 1---------------

    if i try to read file2 i cant read the last char (t) on the file. But it not happens with file1 (once the t char isnt the last one)

    I already tried using feof() but the same occurs.
    I also tried to set
    Code:
    fp--;
    but i cant make it work...

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    Which OS/Compiler - it works here with gcc/linux
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    aze
    aze is offline
    Registered User
    Join Date
    Sep 2003
    Posts
    18
    bcc55 / winxp

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    This should work. If you still have problems with yours, post your code here
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      FILE *fp;
      char buf[BUFSIZ];
      
      if ((fp = fopen("junk1.c", "r")) != NULL)
      {
        while (fgets(buf, sizeof(buf), fp))
        {
          printf ("%s", buf);
        }
        fclose(fp);
      }
      
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Erm, Hammer, don't you need to declare BUFSIZ?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Quote Originally Posted by XSquared
    Erm, Hammer, don't you need to declare BUFSIZ?
    No Well, yes, but it's done for you in stdio.h.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    > don't you need to declare BUFSIZ?
    Don't you know by now that's in stdio.h ?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I think he was trying to make me panic and think I'd made a mistake.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    In all honesty, I didn't know that it was declared in stdio.h. *puts on flame retardant suit*
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

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