reading lines from a text file

Discussion in 'C Programming' started by leorulez@gmail.com, Jan 23, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. Ben Pfaff Guest

    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
     
    Ben Pfaff, Jan 23, 2007
    #2
    1. Advertising

  3. CBFalconer Guest

    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
     
    CBFalconer, Jan 23, 2007
    #3
  4. santosh Guest

    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?
     
    santosh, Jan 23, 2007
    #4
  5. CBFalconer Guest

    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
     
    CBFalconer, Jan 23, 2007
    #5
  6. 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
     
    Robert Gamble, Jan 23, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob
    Replies:
    5
    Views:
    16,634
    tuldom84
    Jul 15, 2012
  2. sahm
    Replies:
    4
    Views:
    42,708
    rel0aded911
    Nov 23, 2009
  3. Joe Wright
    Replies:
    0
    Views:
    544
    Joe Wright
    Jul 27, 2003
  4. Murali
    Replies:
    2
    Views:
    605
    Jerry Coffin
    Mar 9, 2006
  5. Cah Sableng
    Replies:
    0
    Views:
    255
    Cah Sableng
    Apr 23, 2007
Loading...

Share This Page