Read until line break

This is a discussion on Read until line break within the C Programming forums, part of the General Programming Boards category; How can I read a string from a text file until line break (besides using fgetc)?...

  1. #1
    Newbie Crilston's Avatar
    Join Date
    Jun 2005
    Location
    Montreal, Canada
    Posts
    8

    Read until line break

    How can I read a string from a text file until line break (besides using fgetc)?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    fgets
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Newbie Crilston's Avatar
    Join Date
    Jun 2005
    Location
    Montreal, Canada
    Posts
    8
    Quote Originally Posted by Dave_Sinkula
    fgets
    fgets requires a buffer size. What is the maximum length of a single line of a text file (if there is such thing)?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Crilston
    fgets requires a buffer size. What is the maximum length of a single line of a text file (if there is such thing)?
    Generally, whatever size you declare your input buffer to be.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    You could read the line until you reach '\0' with a while loop

    Code:
    while(line[index] != '/0')
    and just read the line through.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    That's pretty much the same as using fgetc().

    If you don't know the size in advance, the best way to do it is to use fgetc().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help with variables, please
    By JOCAAN in forum C++ Programming
    Replies: 39
    Last Post: 12-08-2007, 03:16 PM
  2. how to read each line in text??
    By rameshs in forum C Programming
    Replies: 2
    Last Post: 12-04-2006, 10:46 AM
  3. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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