Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 5data2.txt
This is line 5
This is line 1Result.txt should be:
---------------------
This is line 2
This is line 3
This is line 4But I don't get the required results. Appreciate your assistance.
thanks.

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.

![]() |
![]() |
![]() |

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