reading lines from a text file

L

leorulez

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!
 
B

Ben Pfaff

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.
 
C

CBFalconer

Ben said:
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
 
S

santosh

CBFalconer said:
Ben said:
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:
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?
 
C

CBFalconer

santosh said:
CBFalconer wrote:
.... snip ...

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
 
R

Robert Gamble

santosh said:
CBFalconer said:
Ben said:
(e-mail address removed) 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:
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
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top