Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > reading lines from a text file

Reply
Thread Tools

reading lines from a text file

 
 
leorulez@gmail.com
Guest
Posts: n/a
 
      01-23-2007
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!

 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      01-23-2007
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
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      01-23-2007
Ben Pfaff wrote:
> 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


 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      01-23-2007
CBFalconer wrote:
> Ben Pfaff wrote:
> > 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?

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      01-23-2007
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


 
Reply With Quote
 
Robert Gamble
Guest
Posts: n/a
 
      01-23-2007
santosh wrote:
> CBFalconer wrote:
> > Ben Pfaff wrote:
> > > 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading lines from a text file using The BufferedReader class Bob Java 5 07-14-2012 11:31 PM
reading numbers of words and lines in text file in C++ sahm C++ 4 11-23-2009 07:43 PM
To delete few lines and add few lines at the end of a text file using c program Murali C++ 2 03-09-2006 04:45 PM
Reading just a few lines from a text file tkpmep@hotmail.com Python 6 08-23-2005 11:11 PM
Re: how to read 10 lines from a 200 lines file and write to a new file?? Joe Wright C Programming 0 07-27-2003 08:50 PM



Advertisments