Thread: read a text file with lines of variable length

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    3

    read a text file with lines of variable length

    I'm trying to read a text file with lines of variable length. Is there a way to do that?

    <<split from this thread, don't bump old threads>>

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by raymond
    I'm trying to read a text file with lines of variable length. Is there a way to do that?
    Do you know a maximum line length? If not, are you familiar with dynamic allocation?
    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
    Registered User
    Join Date
    Jun 2006
    Posts
    3
    Thanks for the immediate reply.

    I'm a beginner and looking at this line of code:
    fgets(buf, sizeof(buf), stdin);

    But my input is of variable-length. Can somebody please point me to the correct direction?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Again, do you know a maximum line length, or are you familiar with dynamic allocation?
    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
    Jun 2006
    Posts
    3
    No, I don't know the maximum line length so the answer must be "dynamic allocation." I'll look into that direction.

    If anybody can give some more clues, I'd appreciate it.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You have a few options:

    1 - Use a big buffer, bigger than you think your longest line will be.
    2 - Use a small buffer, and continue reading in a loop until your input contains a newline.

    You can dynamically allocate space if you need to with things like malloc. You can also squish strings together with functions like strcat.


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

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by quzah
    You can also squish strings together with functions like strcat.
    That's amazing. That's exactly how my documentation words the description of strcat() too
    If you understand what you're doing, you're not learning anything.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    And just who do you think wrote all those man pages anyway? Duh!


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read from file problem
    By Martin Kovac in forum C Programming
    Replies: 1
    Last Post: 04-13-2009, 08:33 AM
  2. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  3. Replies: 15
    Last Post: 10-31-2005, 08:29 AM
  4. Replies: 1
    Last Post: 09-10-2005, 06:02 AM