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>>
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?Originally Posted by raymond
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.*
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?
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.*
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.
Hope is the first step on the road to disappointment.
That's amazing. That's exactly how my documentation words the description of strcat() tooOriginally Posted by quzah
If you understand what you're doing, you're not learning anything.
And just who do you think wrote all those man pages anyway? Duh!
Quzah.
Hope is the first step on the road to disappointment.