C++ match two file id's

April 13, 2011 at 15:55:16
Specs: Windows 7
The following code goes into an endless loop. How can I read, compare, write and read the next record


int main()
{
ifstream indata, indata2; // indata is like cin
string num, num2; // variable for input value

indata.open("accounts.dat"); // opens the file
if(!indata) { // file couldn't be opened
cerr << "Error: file could not be opened" << endl;}

indata2.open("transfers.dat"); // opens the file
if(!indata2) { // file couldn't be opened
cerr << "Error: file could not be opened" << endl;

}

indata >> num;
cout << "num is " << num << endl;
while ( !indata.eof() )
{
indata2 >> num2;
while ( !indata2.eof() )
{

if (num == num2)
{
cout<<"This order is valid."<< num2 << endl;
indata2 >> num2;
}
else if (num < num2)
{
cout<<"read next account."<< num << endl;
indata >> num;
}

}
cout << "read until eof is reached" << endl;
indata >> num;

}
indata.close();
cout << "End-of-file reached.." << endl;

indata2.close();
cout << "End-of-file reached.." << endl;


system("PAUSE");
return 0;
}


See More: C++ match two file ids

Report •


#1
April 13, 2011 at 19:59:47
At least one gotcha is with the inner loop; you need to reset eof on file2 with each iteration of file1.

while(file1 ...) {
   ...
   while(file2...) {
  
   }

   file2.clear();
   file2.seekg(0, ios::beg);
}

Your code is hard to read; edit your post and wrap it inside of <PRE> your code </PRE>


Report •
Related Solutions


Ask Question