fstream.getLine() does an abort at newline

S

shyam

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
 
O

Ondra Holub

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' );
 
R

Rolf Magnus

shyam said:
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?
 
S

shyam

The problem is at the end of file

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

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top