fstream如何將兩份相似的文件合並,例如將1.txt和2.txt合並,1.txt和2.txt裡面的內容是表格形式,表頭一樣,內容不一樣,如何將其合並,合並後內容連在一起,表頭只有一份
1.順便寫了一篇blog. http://blog.csdn.net/infoworld/article/details/46758199
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
//最後一個參數是輸出文件.
//支持多余2個的文件合並
//用法,命令行: test.exe 1.txt 2.txt output.txt
int main(int argc, char const *argv[])
{
cout << "usage1: test.exe 1.txt 2.txt output.txt" << endl;
cout << "usage2: test.exe 1.txt 2.txt ... output.txt" << endl;
if(argc <= 3) return -1;
bool first_line_readed = false;
ofstream fout(argv[argc-1], ios::out);
for(int i = 1 ; i< argc - 1; ++i)
{
ifstream fin(argv[i]);
string str;
int line_number = 0;
while(getline(fin,str))
{
if(!line_number)
{
if(!first_line_readed)
{
first_line_readed = true;
str.append("\n");
fout.write(str.c_str(),str.size());
}
}else
{
str.append("\n");
fout.write(str.c_str(),str.size());
}
line_number++;
}
}
cout << "finish..." << argv[argc-1] << endl;
return 0;
}