Computing.Net > Forums > Programming > Line Compare in 2 files 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.

Line Compare in 2 files C++

Reply to Message Icon

Name: nish_r
Date: June 21, 2006 at 04:51:59 Pacific
OS: win
CPU/Ram: 512
Comment:

Hello,

Read line by line from data2.txt, compare with data1.txt lines, if match , 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: nish_r
Date: June 21, 2006 at 06:07:56 Pacific
Reply:

Correction :
------------
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) .

Thx.


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: Line Compare in 2 files C++

How do I iterate in a file? (C++) www.computing.net/answers/programming/how-do-i-iterate-in-a-file-c/13915.html

Finding the total line counts in a file www.computing.net/answers/programming/finding-the-total-line-counts-in-a-file/18939.html

Compare file names in batch file www.computing.net/answers/programming/compare-file-names-in-batch-file/16616.html