fgets is not reading properly

This is a discussion on fgets is not reading properly within the C Programming forums, part of the General Programming Boards category; I'm trying to read 1 line at a time from my file, and in the documentations it said that fgets ...

  1. #1
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    498

    Unhappy fgets is not reading properly

    I'm trying to read 1 line at a time from my file, and in the documentations it said that fgets reads one line at a time and returns a pointer to that line. So I tried it, but it wouldn't stop reading until it reached 255 bytes, even though there were lots of new lines.

    Anybody know anything going wrong? Here is what it looks like:

    Code:
    fgets(stream, 255, file);

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,670
    it should work as expected. unless perhaps the text file was created by a unix program and you're running windows or vice-versa, since they use different values for newlines.



    ITSA
    Socket Library!

  3. #3
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    498
    Thanks.....got it to work. It so happened I was using someone else's text file and trying to load it into my program. When I tried creating my own text file, it worked like it should.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    Post a whole program, not just one line.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    fgets doesn't care if you make the file or if some one else does.


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

  6. #6
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    498
    I'm not going to post the code. It works perfectly. What happened was I was using a text file from another OS. When I rewrote the text file, it worked perfectly. ;-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-05-2009, 02:14 AM
  2. Strange behavior if I use fgets() after fscanf()
    By avi2886 in forum C Programming
    Replies: 5
    Last Post: 02-17-2009, 03:20 PM
  3. Replies: 7
    Last Post: 02-02-2009, 06:27 AM
  4. fgets crashing my program
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2003, 12:08 PM
  5. help with fgets
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-17-2001, 08:18 PM

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