473,324 Members | 2,257 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,324 software developers and data experts.

getline failbit?

179 100+
I'm having trouble with getline function, in the sense that I keep getting an error, and I'm wondering if anyone can help?

The code I'm using is below. What it does is to read a line from one file, and write it out to another. The input files are named:

0node0, 0node1, 0node2, 2node0, 2node1, 2node2 in this example. Any files that aren't found should just be skipped over. The code works fine with 0node0, 0node1, 0node2 and then tries to open 2node0 which seems to work ok. Once it reaches this point however, the failbit is always true, so 'failed' is displayed on screen and I don't understand why. The contents of the file is:

"BB,BA,349.41,4.32
BB,BB,0.00,0.00
BB,BD,50.56,0.78"

So its very simple and there aren't any special characters to mess things up. If anyone could provide me with any suggestions as to why this might be happening it would be much appreciated.

Expand|Select|Wrap|Line Numbers
  1.  // Do for each machine used in the processing
  2.     for (int j = 0; j <= machines; j++)
  3.     {
  4.          // Check for each possible node file
  5.          for (unsigned int i = 0; i < nodes1.size(); i++)
  6.          {
  7.             // Add prefix for distributed processing
  8.             sprintf(fileName ,"%d", j);
  9.             strcat(fileName,"node");                       // Copy 'node' into the file char array               
  10.             sprintf(nodeNum, "%d", i);                 // Convert the NodeID to a char array value
  11.  
  12.             //Try and open the file for input
  13.             inputFile.open(strcat(fileName,nodeNum),std::ios::binary);
  14.             cout << "Checking " << fileName << endl;
  15.             if (!inputFile.is_open())
  16.                continue;                                    // We have to assume that another machine processed this node
  17.  
  18.  
  19.             cout << "Opened : " << fileName << endl;
  20.             // Copy each line into the output file
  21.             while(!inputFile.eof())
  22.             {
  23.                 getline(inputFile, line);
  24.  
  25.                 if(!inputFile.is_open())
  26.                     cout << "File no longer open" << endl;
  27.  
  28.                 if(inputFile.fail())
  29.                     cout << "Failed" << endl;
  30.                 if(inputFile.bad())
  31.                     cout << "Bad Bit" << endl;
  32.                 //cout << line << endl;
  33.                 //outputFile << line << endl;
  34.             }
  35.             processed++;
  36.             inputFile.close();
  37.             inputFile.clear();
  38.             cout << "Closed : " << fileName << endl;
  39. }
Thanks

Ian
Feb 15 '08 #1
4 5843
I'm having trouble with getline function, in the sense that I keep getting an error, and I'm wondering if anyone can help?

The code I'm using is below. What it does is to read a line from one file, and write it out to another. The input files are named:

0node0, 0node1, 0node2, 2node0, 2node1, 2node2 in this example. Any files that aren't found should just be skipped over. The code works fine with 0node0, 0node1, 0node2 and then tries to open 2node0 which seems to work ok. Once it reaches this point however, the failbit is always true, so 'failed' is displayed on screen and I don't understand why. The contents of the file is:

"BB,BA,349.41,4.32
BB,BB,0.00,0.00
BB,BD,50.56,0.78"

So its very simple and there aren't any special characters to mess things up. If anyone could provide me with any suggestions as to why this might be happening it would be much appreciated.

Expand|Select|Wrap|Line Numbers
  1.  // Do for each machine used in the processing
  2.     for (int j = 0; j <= machines; j++)
  3.     {
  4.          // Check for each possible node file
  5.          for (unsigned int i = 0; i < nodes1.size(); i++)
  6.          {
  7.             // Add prefix for distributed processing
  8.             sprintf(fileName ,"%d", j);
  9.             strcat(fileName,"node");                       // Copy 'node' into the file char array               
  10.             sprintf(nodeNum, "%d", i);                 // Convert the NodeID to a char array value
  11.  
  12.             //Try and open the file for input
  13.             inputFile.open(strcat(fileName,nodeNum),std::ios::binary);
  14.             cout << "Checking " << fileName << endl;
  15.             if (!inputFile.is_open())
  16.                continue;                                    // We have to assume that another machine processed this node
  17.  
  18.  
  19.             cout << "Opened : " << fileName << endl;
  20.             // Copy each line into the output file
  21.             while(!inputFile.eof())
  22.             {
  23.                 getline(inputFile, line);
  24.  
  25.                 if(!inputFile.is_open())
  26.                     cout << "File no longer open" << endl;
  27.  
  28.                 if(inputFile.fail())
  29.                     cout << "Failed" << endl;
  30.                 if(inputFile.bad())
  31.                     cout << "Bad Bit" << endl;
  32.                 //cout << line << endl;
  33.                 //outputFile << line << endl;
  34.             }
  35.             processed++;
  36.             inputFile.close();
  37.             inputFile.clear();
  38.             cout << "Closed : " << fileName << endl;
  39. }
Thanks

Ian
Hi lan ,
check return status of the getline, and verify the errno value.
Regards,
Arul
Feb 15 '08 #2
IanWright
179 100+
Hi lan ,
check return status of the getline, and verify the errno value.
Regards,
Arul
Arul, thanks for you quick response. Can I ask how I check the errno value? I've checked on cplusplus.com which i normally use for reference and can't seemed to find anything about it... just mentions the failbit, badbit and eofbit.

The return number from getline is 0.
Feb 15 '08 #3
Arul, thanks for you quick response. Can I ask how I check the errno value? I've checked on cplusplus.com which i normally use for reference and can't seemed to find anything about it... just mentions the failbit, badbit and eofbit.

The return number from getline is 0.
check it at http://rabbit.eng.miami.edu/info/functions/errors.html
Feb 15 '08 #4
IanWright
179 100+
check it at http://rabbit.eng.miami.edu/info/functions/errors.html
Arul,

Thanks for that page. It looks like it could be invaluable in the future. I tested the errno value and it was reported as being 2 (file/directory not found). I thought that was a little odd as obviously the previous line checks to see if the file was opened succesfully.

Therefore I tried resetting the errno to 0, after the file has been opened but before the getline call. When the errno is checked again upon encountering a failbit being set, then the errno is still set to 0...

More strangely, it is always the first file that it tries to open within the outside itteration... so jnodei. The first increment of j fails.. so:

j = 0, i = 0 - Fail
j = 0, i = 1 - OK
j = 0, i = 2 - OK
j = 1, i = 0 - Fail
j = 1, i = 1 - OK
j = 1, i = 2 - OK

I tested the itteration with a slight hack. If an error was detected using inputFile.fail(), then I decrease i and break from my loop (essentially to re-open the same file at the risk of entering an infite loop), and it reads it perfectly.

Has confused me even more now...


Looking at the
Feb 15 '08 #5

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

Similar topics

11
by: John | last post by:
Hello all, I am trying to read in lines into a buffer from a file. Normally I would do this very low-level, but I have come to the conclusion I must stop doing everything the hard way. So, I...
1
by: | last post by:
What is the difference between ios::failbit and ios::badbit? When happen the first and when the second? I try several circumstances to describe when every flag occurs but only ios::failbit I...
10
by: Alex Vinokur | last post by:
What is wrong with small_buffer in program below? I/O getline doesn't read data from file into small (relative to file line size) buffer. ====== foo.cpp ====== #include <cassert> #include...
3
by: Alex Vinokur | last post by:
while (infile_io.getline (buffer, sizeof(buffer)) { // Stuff } How can one know what was a last character in the stream : newline or EOF? -- Alex Vinokur
18
by: Amadeus W. M. | last post by:
I'm trying to read a whole file as a single string, using the getline() function, as in the example below. I can't tell what I'm doing wrong. Tried g++ 3.2, 3.4 and 4.0. Thanks! #include...
6
by: Dave | last post by:
In .Net 2003 if a line, read from a text file is larger than a size parameter, the ifstream getline(buff, sze) put the file pointer to the EOF, so next peek() returns EOF. I saw this problem...
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); } ...
3
by: shyam | last post by:
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...
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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
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.