feof

Discussion in 'C Programming' started by Joriveek, Sep 13, 2005.

  1. Joriveek

    Joriveek Guest

    Hi,

    If I use while(!feof( FileRead )), it reads each line from my input file
    until it encounters an empty space or so,

    but when I have empty spaces within one line, it is not working, it is only
    reading until it finds the first empty space. But I want to read all line
    and then go to next line when the characters start from \n (second line),

    hw can i read this?

    Thanks
    J.
     
    Joriveek, Sep 13, 2005
    #1
    1. Advertising

  2. Joriveek

    Mike Wahler Guest

    "Joriveek" <> wrote in message
    news:dg6q3c$9f6$...
    > Hi,
    >
    > If I use while(!feof( FileRead )), it reads each line from my input file
    > until it encounters an empty space or so,


    No, 'feof()' does not read anything. It simply tells you
    whether the last input request for the stream denoted by
    'FileRead' tried to read *past* end the of the file.

    >
    > but when I have empty spaces within one line, it is not working, it is
    > only reading until it finds the first empty space. But I want to read all
    > line and then go to next line when the characters start from \n (second
    > line),
    >
    > hw can i read this?


    Use 'fgets()'. Be sure to test its return value before
    proceeding with further i/o on the stream (See its
    documentation for the possible return values and their
    meanings).

    Also see the C FAQ for why you should not test for EOF
    before reading, but *after*.
    http://www.eskimo.com/~scs/C-faq/faq.html
    (See section 12)

    -Mike

    >
     
    Mike Wahler, Sep 13, 2005
    #2
    1. Advertising

  3. Joriveek wrote on 13/09/05 :
    > If I use while(!feof( FileRead )), it reads each line from my input file
    > until it encounters an empty space or so,


    Non. It will probably acts like an infinite loop, unless you provide a
    read function. But even in this case, the behaviour is wrong, because
    feof() has a meaning only after an end of reading has occur. feof() and
    ferror() can be used (rarely, actually) to determine the cause of the
    stop.

    > but when I have empty spaces within one line, it is not working, it is only
    > reading until it finds the first empty space. But I want to read all line and
    > then go to next line when the characters start from \n (second line),
    >
    > hw can i read this?


    Read the FAQ, please.

    http://www.eskimo.com/~scs/C-faq/faq.html

    You should use a reading I/O function (fgetc(), fgets(), fread() etc.)
    and test the return value to detect the end of the reading.


    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    I once asked an expert COBOL programmer, how to
    declare local variables in COBOL, the reply was:
    "what is a local variable?"
     
    Emmanuel Delahaye, Sep 13, 2005
    #3
  4. Joriveek

    Joriveek Guest

    Thanks fgets() is of a great help, but it is only reading first line from my
    input file, what should I make this to read all other lines?

    Thanks
    J.


    "Emmanuel Delahaye" <> wrote in message
    news:...
    > Joriveek wrote on 13/09/05 :
    >> If I use while(!feof( FileRead )), it reads each line from my input file
    >> until it encounters an empty space or so,

    >
    > Non. It will probably acts like an infinite loop, unless you provide a
    > read function. But even in this case, the behaviour is wrong, because
    > feof() has a meaning only after an end of reading has occur. feof() and
    > ferror() can be used (rarely, actually) to determine the cause of the
    > stop.
    >
    >> but when I have empty spaces within one line, it is not working, it is
    >> only reading until it finds the first empty space. But I want to read all
    >> line and then go to next line when the characters start from \n (second
    >> line),
    >>
    >> hw can i read this?

    >
    > Read the FAQ, please.
    >
    > http://www.eskimo.com/~scs/C-faq/faq.html
    >
    > You should use a reading I/O function (fgetc(), fgets(), fread() etc.) and
    > test the return value to detect the end of the reading.
    >
    >
    > --
    > Emmanuel
    > The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    > The C-library: http://www.dinkumware.com/refxc.html
    >
    > I once asked an expert COBOL programmer, how to
    > declare local variables in COBOL, the reply was:
    > "what is a local variable?"
    >
    >
     
    Joriveek, Sep 14, 2005
    #4
  5. Joriveek

    Chris Dollin Guest

    Joriveek wrote:

    > Thanks fgets() is of a great help, but it is only reading first line from
    > my input file, what should I make this to read all other lines?


    Call it again?

    [It's very hard to see what you're doing wrong, if we can't see what
    you're doing at all.]

    --
    Chris "electric hedgehog" Dollin
    Logic is rational /re/construction. Life is construction.
     
    Chris Dollin, Sep 14, 2005
    #5
  6. "Joriveek" <> wrote:

    Please don't top-post. Fixed.

    >"Emmanuel Delahaye" <> wrote in message
    >news:...
    >> Joriveek wrote on 13/09/05 :
    >>> If I use while(!feof( FileRead )), it reads each line from my input file
    >>> until it encounters an empty space or so,

    >>
    >> Non. It will probably acts like an infinite loop, unless you provide a
    >> read function. But even in this case, the behaviour is wrong, because
    >> feof() has a meaning only after an end of reading has occur. feof() and
    >> ferror() can be used (rarely, actually) to determine the cause of the
    >> stop.

    <snip>
    >> Read the FAQ, please.
    >>
    >> http://www.eskimo.com/~scs/C-faq/faq.html


    Did you read the FAQ already?

    >> You should use a reading I/O function (fgetc(), fgets(), fread() etc.) and
    >> test the return value to detect the end of the reading.
    >>

    >Thanks fgets() is of a great help, but it is only reading first line from my
    >input file, what should I make this to read all other lines?


    No, you didn't read the FAQ. Sigh.

    General answer:
    get yourself a decent textbook on C, I recommend "The C Programming
    Language" by Brian W. Kernighan and Dennis M. Ritchie, 2nd edition

    Specific answer:
    use a loop

    Oh, and read the FAQ, please.

    Best regards
    --
    Irrwahn Grausewitz ()
    welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
    clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
    clc frequent answers: http://benpfaff.org/writings/clc
     
    Irrwahn Grausewitz, Sep 14, 2005
    #6
  7. Joriveek

    Joriveek Guest

    Thanks I sorted out


    "Irrwahn Grausewitz" <> wrote in message
    news:...
    > "Joriveek" <> wrote:
    >
    > Please don't top-post. Fixed.
    >
    >>"Emmanuel Delahaye" <> wrote in message
    >>news:...
    >>> Joriveek wrote on 13/09/05 :
    >>>> If I use while(!feof( FileRead )), it reads each line from my input
    >>>> file
    >>>> until it encounters an empty space or so,
    >>>
    >>> Non. It will probably acts like an infinite loop, unless you provide a
    >>> read function. But even in this case, the behaviour is wrong, because
    >>> feof() has a meaning only after an end of reading has occur. feof() and
    >>> ferror() can be used (rarely, actually) to determine the cause of the
    >>> stop.

    > <snip>
    >>> Read the FAQ, please.
    >>>
    >>> http://www.eskimo.com/~scs/C-faq/faq.html

    >
    > Did you read the FAQ already?
    >
    >>> You should use a reading I/O function (fgetc(), fgets(), fread() etc.)
    >>> and
    >>> test the return value to detect the end of the reading.
    >>>

    >>Thanks fgets() is of a great help, but it is only reading first line from
    >>my
    >>input file, what should I make this to read all other lines?

    >
    > No, you didn't read the FAQ. Sigh.
    >
    > General answer:
    > get yourself a decent textbook on C, I recommend "The C Programming
    > Language" by Brian W. Kernighan and Dennis M. Ritchie, 2nd edition
    >
    > Specific answer:
    > use a loop
    >
    > Oh, and read the FAQ, please.
    >
    > Best regards
    > --
    > Irrwahn Grausewitz ()
    > welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
    > clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
    > clc frequent answers: http://benpfaff.org/writings/clc
     
    Joriveek, Sep 14, 2005
    #7
  8. Joriveek wrote on 14/09/05 :
    > Thanks fgets() is of a great help, but it is only reading first line from my
    > input file, what should I make this to read all other lines?


    Consider a code structure like while()...

    Time to open a C-book...

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "There are 10 types of people in the world today;
    those that understand binary, and those that dont."
     
    Emmanuel Delahaye, Sep 14, 2005
    #8
  9. "Joriveek" <> writes:
    > Thanks I sorted out


    Great.

    Again, please don't top-post. Your response belongs below any quoted
    text, and you should trim anything that isn't relevant. The idea is
    that each followup should be readable by itself, from top to bottom.
    See nearly any article this newsgroup for an example.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Sep 14, 2005
    #9
    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. Mantorok Redgormor

    feof usage

    Mantorok Redgormor, Sep 20, 2003, in forum: C Programming
    Replies:
    28
    Views:
    1,147
    Dave Thompson
    Sep 29, 2003
  2. question about gets() and feof().

    , Sep 16, 2005, in forum: C Programming
    Replies:
    3
    Views:
    362
    Simon Biber
    Sep 16, 2005
  3. ehui928

    A question about fscanf and feof !

    ehui928, Oct 15, 2006, in forum: C Programming
    Replies:
    7
    Views:
    3,152
    Joe Wright
    Nov 7, 2006
  4. rCs

    EOF vs. feof() and ferror()

    rCs, Oct 31, 2006, in forum: C Programming
    Replies:
    8
    Views:
    1,764
    Barry Schwarz
    Nov 1, 2006
  5. ericunfuk

    feof(), fseek(), fread()

    ericunfuk, Mar 24, 2007, in forum: C Programming
    Replies:
    20
    Views:
    2,435
    Joe Wright
    Mar 27, 2007
Loading...

Share This Page