getline failbit?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • IanWright
    New Member
    • Jan 2008
    • 179

    getline failbit?

    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.7 8"

    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.

    [CODE=cpp] // Do for each machine used in the processing
    for (int j = 0; j <= machines; j++)
    {
    // Check for each possible node file
    for (unsigned int i = 0; i < nodes1.size(); i++)
    {
    // Add prefix for distributed processing
    sprintf(fileNam e ,"%d", j);
    strcat(fileName ,"node"); // Copy 'node' into the file char array
    sprintf(nodeNum , "%d", i); // Convert the NodeID to a char array value

    //Try and open the file for input
    inputFile.open( strcat(fileName ,nodeNum),std:: ios::binary);
    cout << "Checking " << fileName << endl;
    if (!inputFile.is_ open())
    continue; // We have to assume that another machine processed this node


    cout << "Opened : " << fileName << endl;
    // Copy each line into the output file
    while(!inputFil e.eof())
    {
    getline(inputFi le, line);

    if(!inputFile.i s_open())
    cout << "File no longer open" << endl;

    if(inputFile.fa il())
    cout << "Failed" << endl;
    if(inputFile.ba d())
    cout << "Bad Bit" << endl;
    //cout << line << endl;
    //outputFile << line << endl;
    }
    processed++;
    inputFile.close ();
    inputFile.clear ();
    cout << "Closed : " << fileName << endl;
    }[/CODE]

    Thanks

    Ian
  • Arulmurugan
    New Member
    • Jan 2008
    • 90

    #2
    Originally posted by IanWright
    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.7 8"

    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.

    [CODE=cpp] // Do for each machine used in the processing
    for (int j = 0; j <= machines; j++)
    {
    // Check for each possible node file
    for (unsigned int i = 0; i < nodes1.size(); i++)
    {
    // Add prefix for distributed processing
    sprintf(fileNam e ,"%d", j);
    strcat(fileName ,"node"); // Copy 'node' into the file char array
    sprintf(nodeNum , "%d", i); // Convert the NodeID to a char array value

    //Try and open the file for input
    inputFile.open( strcat(fileName ,nodeNum),std:: ios::binary);
    cout << "Checking " << fileName << endl;
    if (!inputFile.is_ open())
    continue; // We have to assume that another machine processed this node


    cout << "Opened : " << fileName << endl;
    // Copy each line into the output file
    while(!inputFil e.eof())
    {
    getline(inputFi le, line);

    if(!inputFile.i s_open())
    cout << "File no longer open" << endl;

    if(inputFile.fa il())
    cout << "Failed" << endl;
    if(inputFile.ba d())
    cout << "Bad Bit" << endl;
    //cout << line << endl;
    //outputFile << line << endl;
    }
    processed++;
    inputFile.close ();
    inputFile.clear ();
    cout << "Closed : " << fileName << endl;
    }[/CODE]

    Thanks

    Ian
    Hi lan ,
    check return status of the getline, and verify the errno value.
    Regards,
    Arul

    Comment

    • IanWright
      New Member
      • Jan 2008
      • 179

      #3
      Originally posted by Arulmurugan
      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.

      Comment

      • Arulmurugan
        New Member
        • Jan 2008
        • 90

        #4
        Originally posted by IanWright
        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.miam i.edu/info/functions/errors.html

        Comment

        • IanWright
          New Member
          • Jan 2008
          • 179

          #5
          Originally posted by Arulmurugan
          check it at http://rabbit.eng.miam i.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

          Comment

          Working...