473,320 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Reading lines from a file

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
7 3360
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
"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
"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
"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

"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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Boris Boutillier | last post by:
HI all, I came across a strange behaviour with read method, and I'm not sure if this is a filesystem problem or a misuse of this method. I do the simple following thing : f = open...
3
by: Steven Burn | last post by:
The application; Service on my webserver that allows a user to upload their HOSTS file for functions to verify the contents are still valid. Uses; 1. XMLHTTP (MSXML2) 2. FileSystemObject...
6
by: tkpmep | last post by:
I have a text file with many hundreds of lines of data. The data of interest to me, however, resides at the bottom of the file, in the last 20 lines. Right now, I read the entire file and discard...
20
by: sahukar praveen | last post by:
Hello, I have a question. I try to print a ascii file in reverse order( bottom-top). Here is the logic. 1. Go to the botton of the file fseek(). move one character back to avoid the EOF. 2....
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
11
by: Girish Sahani | last post by:
I wrote the following code to concatenate every 2 keys of a dictionary and their corresponding values. e.g if i have tiDict1 = tiDict1 = {'a':,'b':} i should get tiDict2={'ab':} and similarly for...
12
by: Yi Xing | last post by:
Hi All, I want to read specific lines of a huge txt file (I know the line #). Each line might have different sizes. Is there a convenient and fast way of doing this in Python? Thanks. Yi Xing
2
by: fool | last post by:
Dear group, I am a beginner in php and I was little bit experience in C language. I want to read a file's content and delete the first line of the file and display those lines which has got...
3
by: The Cool Giraffe | last post by:
Regarding the following code i have a problem. void read () { fstream file; ios::open_mode opMode = ios::in; file.open ("some.txt", opMode); char *ch = new char; vector <charv; while...
2
by: rka77 | last post by:
Hi, I am trying to make a Python2.6 script on a Win32 that will read all the text files stored in a directory and print only the lines containing actual data. A sample file - Set : 1 Date:...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.