Computing.Net > Forums > Programming > Read Line by Line and Match C++

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Read Line by Line and Match C++

Reply to Message Icon

Name: nish_r
Date: June 26, 2006 at 23:54:43 Pacific
OS: XP
CPU/Ram: 512
Comment:

Hello,

Read line by line from data2.txt, compare with data1.txt lines, if match , do nothing (NOT remove) , else append to a new file (result.txt) .
mathces are exact string matches, both are text files.

My code as follows.
-------------------
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream ifs1("data1.txt");
ifstream ifs2("data2.txt");
ofstream ofs("result.txt");
string line1;
string line2;
while (!ifs2.eof())
{
getline(ifs2,line2);
while (!ifs1.eof()) {
getline(ifs1,line1);
if (line1!=line2) {
ofs << line1;
ofs << "\n";
}
else {
}
}
}
return 0;
}
-------------------------

data1.txt :
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5

data2.txt
This is line 5
This is line 1

Result.txt should be:
---------------------
This is line 2
This is line 3
This is line 4

But I don't get the required results. Appreciate your assistance.

thanks.




Sponsored Link
Ads by Google

Response Number 1
Name: wizard-fred
Date: June 27, 2006 at 23:21:44 Pacific
Reply:

If both sets of lines are not sorted order then you have to do multiple passes to find the matches. If they are sorted then you must do some sort of buffering to keep then in sync.


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Read Line by Line and Match C++

File read line by line and compare www.computing.net/answers/programming/file-read-line-by-line-and-compare-/14450.html

reading lines from text file (java) www.computing.net/answers/programming/reading-lines-from-text-file-java/12276.html

Reading ctrl-D from cin in C++ www.computing.net/answers/programming/reading-ctrld-from-cin-in-c/3430.html