By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,370 Members | 1,590 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,370 IT Pros & Developers. It's quick & easy.

Reading lines from a file

P: n/a
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!

--
Regards,
Webster
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Mon, 26 Jan 2004 02:59:45 GMT, "Webster" <no***@nowhere.net> wrote
in comp.lang.c++:
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()

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #2

P: n/a
"Jack Klein" <ja*******@spamcop.net> wrote in message
news:s7********************************@4ax.com...

fgets()


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

--
Regards,
Webster
Jul 22 '05 #3

P: n/a
"Webster" <no***@nowhere.net> wrote in message
news:GC******************@twister01.bloor.is.net.c able.rogers.com...
"Jack Klein" <ja*******@spamcop.net> wrote in message
news:s7********************************@4ax.com...

fgets()

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
Jul 22 '05 #4

P: n/a
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:F7*******************@newsread1.news.pas.eart hlink.net...
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:

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?

--
Regards,
Webster
Jul 22 '05 #5

P: n/a

"Webster" <no***@nowhere.net> wrote in message
news:vj******************@twister01.bloor.is.net.c able.rogers.com...
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:F7*******************@newsread1.news.pas.eart hlink.net...
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:


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?


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
Jul 22 '05 #6

P: n/a
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:2V*****************@newsread2.news.pas.earthl ink.net...
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!
--
Regards,
Webster
Jul 22 '05 #7

P: n/a
Webster wrote:

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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.