473,413 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

fstream.getLine() does an abort at newline

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

Jan 15 '07 #1
3 3625

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

Jan 15 '07 #2
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?

Jan 15 '07 #3
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?
Jan 15 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Danny Anderson | last post by:
Hola- I didn't get any responses on a previous post, so I am trying to reword my problem and post compile-able code that exhibits the behavior I am describing. On the second iteration of the...
16
by: JKop | last post by:
Before I resort to the "fopen" family, can some-one please direct me to a tutorial on the net about the "fstream" family. I've gone through about 10 references and they're all complete bullshit....
7
by: Computer Whizz | last post by:
Hi, I was just wondering if someone would like to comment on these two issues. I had a 15 minute wander around some sites and was curious about loading files (plain ASCII I think will do for a...
2
by: Peter Gordon | last post by:
Is using getline with a fstream reference covered in the standard? I have three C++ compilers on my system. Two of them compile the code below without warnings. The third returns the following...
2
by: Mark P | last post by:
Consider the following snippet of code to read lines from a text file: ifstream file_stream( "some_file.txt"); string read_line; while( file_stream) { getline( file_stream, read_line); } ...
6
by: wiso | last post by:
My problem is this (from: http://www.cplusplus.com/ref/iostream/fstream/open.html) #include <fstream> using namespace std; int main() { fstream f;
2
by: jjcp | last post by:
I would like to say thanks in advance for insight anyone can shed on this for me. Long story short from time to time I need to us C++ to take a list of file and make an index out of them in...
2
by: beepa | last post by:
As you will be able to see I am fairly new at this. Here is the part I'm having problems figuring out: The file I'm inputing from is formated like this: firstName lastName postion name (one or...
11
by: rory | last post by:
I am reading a binary file and I want to search it for a string. The only problem is that failbit gets set after only a few calls to getline() so it never reaches the end of the file where the...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.