C++輸出兩個文本文件不同的行


假設A,B兩個文件,A文件中100行字符,B文件中200行字符。輸出B中與A中行內容不同的行。

// find different lines from two text 

#include "iostream"
#include "fstream"
#include "string"
#include "vector"

int main()
{
	using namespace std;
	string filename1,filename2;
	cout << "Enter the first file name:";
	cin >> filename1;
	cout << "Enter the second file name:";
	cin >> filename2;
	ifstream fin1(filename1.c_str());
	ifstream fin2(filename2.c_str());
	string temp;
	vector<string> a;
	vector<string> b;

	while(getline(fin1,temp))
	{
		a.push_back(temp);
	}
	while(getline(fin2,temp))
	{
		b.push_back(temp);
	}
	fin1.close();
	fin2.close();

	int sum = 0;

	for(int t = 0; t < b.size();t++)
	{
		int count = 0;
		for(int s = 0;s < a.size() ;s++)
		{
			if(b[t].compare(a[s])==0)
				break;
			else
				count++;
		}
		if(count >= a.size())
		{
			cout << b[t] << endl;
			sum++;
		}
			
	}
	cout << "sum = " << sum << endl;
	return 0;
}



發佈了25 篇原創文章 · 獲贊 4 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章