There is such an educational task:

Create two text files, remove all numbers contained in both files at the same time from the first file. Additional arrays and files do not use.

I create files like this:

ofstream fout("1.txt"); fout << "619"; fout.close(); system("pause"); ofstream fout1("2.txt"); fout1 << "618"; fout1.close(); system("pause"); 

Next, you need to compare the data and delete. How to implement it?

  • For convenience, you can also make a text file of any format, for example, make one number, or you can separate each number with a space. - Maxim Shinkarev
  • Open both files, read into set containers (or, say, vector and sort), use the set_difference algorithm ... - Harry
  • @Harry Could you help me with the code? - Maxim Shinkarev
  • @Harry I read the strings in a set array. ifstream f2 ("21.txt"); getline (f2, s); ss << s; while (ss) {ss >> value; temp2.insert (value); } Now they need to be sorted? How to sort and for what? - Maxim Shinkarev

1 answer 1

Well, something like this:

 int main(int argc, const char * argv[]) { { ofstream out1("file1"); for(int i = 0; i < 500; ++i) out1 << rand() << "\n"; ofstream out2("file2"); for(int i = 0; i < 500; ++i) out2 << rand() << "\n"; } vector<int> s1, s2, s3; { int n; ifstream in1("file1"); while(in1 >> n) s1.push_back(n); ifstream in2("file2"); while(in2 >> n) s2.push_back(n); } sort(s1.begin(),s1.end()); sort(s2.begin(),s2.end()); set_difference(s1.begin(),s1.end(),s2.begin(),s2.end(),back_inserter(s3)); { ofstream out("file3"); for(int n: s3) out << n << "\n"; } }