473,324 Members | 2,178 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,324 software developers and data experts.

reading lines from a text file

Hi all

I need to read the number of non-empty lines in a text file

char c[100];
int i=0,j=0;
while( !feof( fp1 ) )
{

fgets( c , 100 , fp1);
while( ( c[ i ] == ' \n' )
j++;
i++;

}
printf("number of non-empty lines=%d", i-j);

For some reason this code is not working. The c[ i ] == ' \n' is not
getting satisfied at all. Is there anything that I am missing?

Thanks in advance!

Jan 23 '07 #1
5 4513
le******@gmail.com writes:
while( !feof( fp1 ) )
{

fgets( c , 100 , fp1);
while( ( c[ i ] == ' \n' )
j++;
i++;

}
I don't think you understand I/O very well. First of all,
there's what the FAQ says about feof:

12.2: Why does the code

while(!feof(infp)) {
fgets(buf, MAXLINE, infp);
fputs(buf, outfp);
}

copy the last line twice?

A: In C, end-of-file is only indicated *after* an input routine has
tried to read, and failed. (In other words, C's I/O is not like
Pascal's.) Usually, you should just check the return value of
the input routine (in this case, fgets() will return NULL on end-
of-file); often, you don't need to use feof() at all.

References: K&R2 Sec. 7.6 p. 164; ISO Sec. 7.9.3, Sec. 7.9.7.1,
Sec. 7.9.10.2; H&S Sec. 15.14 p. 382.

Second, consider your loop:
while( ( c[ i ] == ' \n' )
j++;
If the condition on the "if" is ever true, the loop will never
terminate.

Think about these problems and you should be able to discover a
solution.
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Jan 23 '07 #2
Ben Pfaff wrote:
le******@gmail.com writes:
>while( !feof( fp1 ) )
{

fgets( c , 100 , fp1);
while( ( c[ i ] == ' \n' )
j++;
i++;

}

I don't think you understand I/O very well. First of all,
there's what the FAQ says about feof:

12.2: Why does the code

while(!feof(infp)) {
fgets(buf, MAXLINE, infp);
fputs(buf, outfp);
}

copy the last line twice?

A: In C, end-of-file is only indicated *after* an input routine has
tried to read, and failed. (In other words, C's I/O is not like
Pascal's.) Usually, you should just check the return value of
the input routine (in this case, fgets() will return NULL on end-
of-file); often, you don't need to use feof() at all.

References: K&R2 Sec. 7.6 p. 164; ISO Sec. 7.9.3, Sec. 7.9.7.1,
Sec. 7.9.10.2; H&S Sec. 15.14 p. 382.

Second, consider your loop:
> while( ( c[ i ] == ' \n' )
j++;

If the condition on the "if" is ever true, the loop will never
terminate.

Think about these problems and you should be able to discover a
solution.
In addition he doesn't need an input buffer. A single integer and
getc access will do nicely.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Jan 23 '07 #3
CBFalconer wrote:
Ben Pfaff wrote:
le******@gmail.com writes:
while( !feof( fp1 ) )
{

fgets( c , 100 , fp1);
while( ( c[ i ] == ' \n' )
j++;
i++;

}
I don't think you understand I/O very well. First of all,
there's what the FAQ says about feof:
<snip>
Second, consider your loop:
while( ( c[ i ] == ' \n' )
j++;
If the condition on the "if" is ever true, the loop will never
terminate.

Think about these problems and you should be able to discover a
solution.

In addition he doesn't need an input buffer. A single integer and
getc access will do nicely.
Where will he store the line then?

Jan 23 '07 #4
santosh wrote:
CBFalconer wrote:
Ben Pfaff wrote:
le******@gmail.com writes:
>
>while( !feof( fp1 ) )
> {
>>
> fgets( c , 100 , fp1);
> while( ( c[ i ] == ' \n' )
> j++;
> i++;
>>
> }
>
I don't think you understand I/O very well. First of all,
there's what the FAQ says about feof:
<snip>
Second, consider your loop:
>
> while( ( c[ i ] == ' \n' )
> j++;
>
If the condition on the "if" is ever true, the loop will never
terminate.
>
Think about these problems and you should be able to discover a
solution.
In addition he doesn't need an input buffer. A single integer and
getc access will do nicely.

Where will he store the line then?
He doesn't need to store lines if he is just looking for a count.

Robert Gamble

Jan 23 '07 #5
santosh wrote:
CBFalconer wrote:
.... snip ...
>>
In addition he doesn't need an input buffer. A single integer and
getc access will do nicely.

Where will he store the line then?
The OP posted:
"I need to read the number of non-empty lines in a text file"

which doesn't require storing any lines.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Jan 23 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Job Lot | last post by:
I have tab delimited text file which gets populated on daily basis via automated process. New entry is written at the bottom. I need to create a utility which makes a copy of this file with 10 most...
7
by: jamait | last post by:
Hi all, I m trying to read in a text file into a datatable... Not sure on how to split up the information though, regex or substrings...? sample: Col1 Col2 ...
18
by: Michael | last post by:
Hi, I moved to c++ from c, and wanted to know what the best way to read data from files is in c++. Any thoughts? fscanf() is possible but fairly painful! Regards Michael
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...
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...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
3
by: bbepristis | last post by:
Hey all I have this code that reads from one text file writes to another unless im on a certian line then it writes the new data however it only seems to do about 40 lines then quits and I cant...
3
by: lizii | last post by:
i have a file - which on each line has some data i need to fill into a box - now although reading in the data is simple enough and putting it in the correct box will be no problem, as i can just...
111
by: Tonio Cartonio | last post by:
I have to read characters from stdin and save them in a string. The problem is that I don't know how much characters will be read. Francesco -- ------------------------------------- ...
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...
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...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.