473,320 Members | 1,953 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,320 software developers and data experts.

Inputting From an External File and Testing for End of File

Hello,
Several Weeks ago I asked a question about testing for the end of an input file.
I have been using this method pretty well for inputting information from an external file. I am using C++.
Expand|Select|Wrap|Line Numbers
  1. char line[50];
  2. while(file>>line){
Where line is a c-string and file is an input stream. But this only reads until the first space it encounters. I now want to try to take in an entire line, regardless of what it contains (such as whitespace) as a string and then parse the string later as appropriate.
When I try
Expand|Select|Wrap|Line Numbers
  1.  string line;
  2. while(getline(file, line)){ 
Is working fine until my input file ( .txt) has 2 or more white lines at the end or if I have a white line in the middle of the file.
If there is 0 or 1 white line at the end, I have no problems; however when I have 2 or more empty lines I run into trouble and my program crashes.

Could you please explain what is going wrong? What am I missing?

Thanks You
Nov 5 '07 #1
2 1705
weaknessforcats
9,208 Expert Mod 8TB
If getline() extracts no elements it sets the fail bit. Once the failbit is set all further calls to that istream fail since the fail bit is the first thing that getline() checks.

Probably you need to check that the rdstate is true and the eofbit is false. The rdstate will be false if the fail bit ti set. There is an isteream::eof() function that checks this. This function returns false if rdstate is true and eofbit is false. All other combonations return true.

If the rdstate is false and the eofbit is false, then you can clear the failbit and make rdstate true by calling the istream::clear() method.
Nov 5 '07 #2
Thanks,
I figured out what was causing the problem. The
Expand|Select|Wrap|Line Numbers
  1. while(getline(file,line)){
wasn't the problem. What was causing the program to crash was inside the while loop. I didn't realize that getline will actually return a string with nothing in it for an empty line. As I was parsing the data, if there was a white space, the string length was (previously not so obvious to me) 0. I had a couple of functions that assumed that the string length was automatically greater than 0. So I included a
Expand|Select|Wrap|Line Numbers
  1. if(line.length){
and this resolved the problem. I can now have as many white lines as I want where ever I want.
Nov 5 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Erohsik | last post by:
I am using external tables to load data from the data file. But the log files are getting created in the datafile's directory (because of CREATE OR REPLACE DIRECTORY ...) But I want those log...
5
by: Chris | last post by:
I have a meetings section I'm developing on our intranet. Using PHP/MySQL. Meeting info and Meeting docs reside on 2 related tables in the db. Users may want to upload anywhere from 1 to 10 or...
20
by: Mark Anderson | last post by:
Hi, I have this in an external JS library: ///////////////////////// function addMyEvent(){ var obj; if(document.attachEvent) { obj = document.getElementsByTagName('img'); for...
4
by: Erohsik | last post by:
I am using external tables to load data from the data file. But the log files are getting created in the datafile's directory (because of CREATE OR REPLACE DIRECTORY ...) But I want those log...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.