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
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
Comment