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>>
Printable View
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>>
Do you know a maximum line length? If not, are you familiar with dynamic allocation?Quote:
Originally Posted by raymond
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?
Again, do you know a maximum line length, or are you familiar with dynamic allocation?
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.
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.
That's amazing. That's exactly how my documentation words the description of strcat() too ;)Quote:
Originally Posted by quzah
And just who do you think wrote all those man pages anyway? Duh! :rolleyes:
Quzah.