fstream.getLine() does an abort at newline

Discussion in 'C++' started by shyam, Jan 15, 2007.

  1. shyam

    shyam Guest

    Hi All

    I want to know if there is any problem with using
    fstream.getLine(char*, int ) function.

    My problem is that when I read a file using it, the program aborts when
    it reads the terminating newline of a file.

    This happens if I set the exception bits using
    exceptions(fstream::badbit | fstream::failbit | fstream::eofbit ).
    Otherwise it does not happen.

    Is there any alternative??

    thanks in advance
     
    shyam, Jan 15, 2007
    #1
    1. Advertising

  2. shyam

    Ondra Holub Guest

    shyam napsal:
    > Hi All
    >
    > I want to know if there is any problem with using
    > fstream.getLine(char*, int ) function.
    >
    > My problem is that when I read a file using it, the program aborts when
    > it reads the terminating newline of a file.
    >
    > This happens if I set the exception bits using
    > exceptions(fstream::badbit | fstream::failbit | fstream::eofbit ).
    > Otherwise it does not happen.
    >
    > Is there any alternative??
    >
    > thanks in advance


    You can use getline from <string>:

    #include <string>
    istream& getline( istream& is, string& s, char delimiter = '\n' );
     
    Ondra Holub, Jan 15, 2007
    #2
    1. Advertising

  3. shyam

    Rolf Magnus Guest

    shyam wrote:

    > Hi All
    >
    > I want to know if there is any problem with using
    > fstream.getLine(char*, int ) function.


    The function is called 'getline'. Case matters in C++.

    > My problem is that when I read a file using it, the program aborts when
    > it reads the terminating newline of a file.
    >
    > This happens if I set the exception bits using
    > exceptions(fstream::badbit | fstream::failbit | fstream::eofbit ).
    > Otherwise it does not happen.


    Do you catch those exceptions?

    > Is there any alternative??


    Why aren't you using std::getline with std::string instead?
     
    Rolf Magnus, Jan 15, 2007
    #3
  4. shyam

    shyam Guest

    The problem is at the end of file

    Since I am setting the bits for eofbit so at EOF the function throws
    failure exception


    Rolf Magnus wrote:
    > shyam wrote:
    >
    > > Hi All
    > >
    > > I want to know if there is any problem with using
    > > fstream.getLine(char*, int ) function.

    >
    > The function is called 'getline'. Case matters in C++.
    >
    > > My problem is that when I read a file using it, the program aborts when
    > > it reads the terminating newline of a file.
    > >
    > > This happens if I set the exception bits using
    > > exceptions(fstream::badbit | fstream::failbit | fstream::eofbit ).
    > > Otherwise it does not happen.

    >
    > Do you catch those exceptions?
    >
    > > Is there any alternative??

    >
    > Why aren't you using std::getline with std::string instead?
     
    shyam, Jan 15, 2007
    #4
    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. schaf
    Replies:
    0
    Views:
    438
    schaf
    May 29, 2005
  2. Armando
    Replies:
    6
    Views:
    776
    Armando
    Jan 29, 2004
  3. Peter Gordon
    Replies:
    2
    Views:
    4,594
    Peter Gordon
    Mar 15, 2005
  4. rory
    Replies:
    15
    Views:
    3,837
    James Kanze
    Jan 25, 2008
  5. Disc Magnet
    Replies:
    3
    Views:
    1,246
    James Kanze
    Mar 5, 2010
Loading...

Share This Page