| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Comparing files one line at a time
Hi,
I am attempting to compare two files one line at a time and then out put a match to a third file. For example: file 1: gsmith 850 hsmith 1520 grock 2568 gmist 45 file 2: hsmith 1520 grock 2568 gmist 45 gsmith 900 As you can see, the files contain basically the same information (col 1 is a name and col 2 is a cash figure) but will never be in the same order, and are actually much larger than what is shown here. What I am attempting to do is stream file 1 in one line at a time and then search through file 2 for a matching name and then out put the match as well as the cash figure from both file 1 and file 2 into a seperate output file. Below is what I have coded so far: #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; int main() { string snam; string opsnam; double cash; double opcash; double dif; ifstream stream1("f:/files/c1.txt", ios::in | ios::binary); ifstream stream2("f:/files/c2.txt", ios::in | ios::binary); ofstream output("f:/files/outputdata.txt", ios: ut | ios::trunc);output << "Account Name Cash1 Cash2 Difference" << endl; while(!stream1.eof()) { stream1 >> snam >> cash; while(!stream2.eof()) { stream2 >> opsnam >> opcash; if(snam == opsnam){ dif = (cash - opcash); output << snam << " " << cash << " " << opcash << " " << dif << endl; } } } return 0; } How can I stop stream1 once the name and cash figure on line 1 are read, search stream2 for a matching name and then return to stream1 at the next line to start the process again. Currenty the file stops looping after the first match is found (after it finds a match for the data on line 1 of file 1). I've tried to do this with both "for" and "while" loops. Any help is appreciated. |
|
#2
|
|||
|
|||
|
You need to restart the second file before going into the second loop. Have a look at the function rewind which I think does this, or failing all else just reopen the stream. (Sure someone has a better way than this as I know rewind is c and we are dealing with c++ streams).
If you are using those example files what is happening is the first one gsmith is searched for which is at the end of the second file. The match is found but then the second file always returns eof. Hope this helps, -KM- |
|
#3
|
|||
|
|||
|
Thanks for your help Kode_monkey. You definately set me in the right direction.
I've solved the problem using the seekg() function in conjuction with the clear() function. seekg() is setting the second stream back to the begging of the file and the clear() function is removing the eof error created once the stream reaches the end of the file. Here is the code: #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; int main() { string snam; string opsnam; double cash; double opcash; double dif; int offset; offset = 0; ifstream stream1("f:/files/c1.txt", ios::in | ios::binary); ifstream stream2("f:/files/c2.txt", ios::in | ios::binary); ofstream output("f:/files/outputdata.txt", ios: ut | ios::trunc);output << "Account Name Cash1 Cash2 Difference" << endl; while(!stream1.eof()) { stream1 >> snam >> cash; stream2.seekg(offset, ios::beg); while(!stream2.eof()) { stream2 >> opsnam >> opcash; if(snam == opsnam){ dif = (cash - opcash); output << snam << " " << cash << " " << opcash << " " << dif << endl; } } stream2.clear(); } return 0; } |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Comparing files one line at a time |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|