Reading lines from a file

W

Webster

Hello,

I have to read a file with a single "command" per line. But how do I read to
the end of the line?? I am using C style filepointers (not by choice :( )

Thanks for any help!
 
J

Jack Klein

Hello,

I have to read a file with a single "command" per line. But how do I read to
the end of the line?? I am using C style filepointers (not by choice :( )

Thanks for any help!

fgets()
 
M

Mike Wahler

Webster said:
But what about the max number of characters to read?? How will I know where
the end of the line is??

What about you actually looking up the function Jack referred to?
Its documentation answers both of those questions:

==================================================================
ISO/IEC 9899:1999 (E)

7.19.7.2 The fgets function

Synopsis

1 #include <stdio.h>
char *fgets(char * restrict s, int n,
FILE * restrict stream);

Description

2 The fgets function reads at most one less than the number of
characters specified by n from the stream pointed to by stream
into the array pointed to by s. No additional characters are
read after a new-line character (which is retained) or after
end-of-file. A null character is written immediately after the
last character read into the array.

Returns

3 The fgets function returns s if successful. If end-of-file is
encountered and no characters have been read into the array, the
contents of the array remain unchanged and a null pointer is
returned. If a read error occurs during the operation, the array
contents are indeterminate and a null pointer is returned.
==================================================================

-Mike
 
W

Webster

What about you actually looking up the function Jack referred to?
Its documentation answers both of those questions:

I had.
The documentation describes the arguments, but does not answer my questions.
1 #include <stdio.h>
char *fgets(char * restrict s, int n,
FILE * restrict stream);

Description

2 The fgets function reads at most one less than the number of
characters specified by n from the stream pointed to by stream
into the array pointed to by s. No additional characters are
read after a new-line character (which is retained) or after
end-of-file. A null character is written immediately after the
last character read into the array.

I want to read to the end of the line, so what value should I use for n
since I don't know how long the line is?? If my line is 1000 chars long, and
I make n=100, then it is not read to the end of the line no?
 
M

Mike Wahler

Webster said:
I had.
The documentation describes the arguments, but does not answer my questions.

I want to read to the end of the line, so what value should I use for n
since I don't know how long the line is?? If my line is 1000 chars long, and
I make n=100, then it is not read to the end of the line no?

If a line is longer than your best estimate (which you'd use to
size the array), then simply call 'fgets()' again, until you do
hit a newline. You can determine if a newline has been stored
in the array for each call by searching for it with e.g. 'strchr()'.

If you absolutely must know the length of the longest line in advance,
the only way to do it is to read the file one character at a time,
counting newline characters as you go. Then you can allocate a large
enough array (don't forget to leave room for the newline and the
string terminator), close and reopen (or rewind) the file, and then
read a line at a time. And don't forget to free the array when you're
done with it.

-Mike
 
W

Webster

If a line is longer than your best estimate (which you'd use to
size the array), then simply call 'fgets()' again, until you do
hit a newline. You can determine if a newline has been stored
in the array for each call by searching for it with e.g. 'strchr()'.

If you absolutely must know the length of the longest line in advance,
the only way to do it is to read the file one character at a time,
counting newline characters as you go. Then you can allocate a large
enough array (don't forget to leave room for the newline and the
string terminator), close and reopen (or rewind) the file, and then
read a line at a time. And don't forget to free the array when you're
done with it.

Cool.. that makes sense.. thanks for the help!
 
K

Kevin Goodsell

Webster said:
I want to read to the end of the line, so what value should I use for n
since I don't know how long the line is?? If my line is 1000 chars long, and
I make n=100, then it is not read to the end of the line no?

To overcome that you can roll your own line-reading function using
getc(). You'll need to store each read char into a dynamically sized
sequence[1], possibly a std::vector or std::string. You can also
malloc() and realloc() your own buffer (or use new[] and delete[] along
with copying, or use std::allocator), but I wouldn't recommend it.

[1]Technically you don't *have* to use a dynamically sized buffer to
store the chars, but if you are going to use a fixed size buffer then
you might as well just use fgets().

-Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top