问题: 编写一个程序,它打开两个文本文件进行输入, 打开一个文本文件进行输出。该程序将两个输入文件中对应的行并接起来,并用空格分割,然后将结果写入到输出文件中。如果一个文件比另一个短,则将较长文件中余下的行直接复制到输出文件中。例如,假设第一个输入文件的内容如下:
eggs kites donuts
balloons hammers
stones
而第二个输入文件的内容如下:
zero lassitude
finance drama
则得到的文件的内容将如下:
eggs kites donuts zero lassitude
balloons hammers finance drama
stones
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
using namespace std;
if (argc<= 3)
{
cout << "please input two source file name and one dest file name";
return 0;
}
ifstream src1File(argv[1]);
ifstream src2File(argv[2]);
ofstream destFile(argv[3]);
if (!src1File.is_open() || !src2File.is_open() || !destFile.is_open())
{
cout << "open file failed.";
return 0;
}
string strLine1, strLine2;
while (true)
{
if (!src1File.eof())
{
getline(src1File, strLine1);
if (!src2File.eof())
{
destFile << strLine1 << " ";
}
else
{
destFile << strLine1 << endl;
}
}
if (!src2File.eof())
{
getline(src2File, strLine2);
destFile << strLine2 << endl;
}
if (src1File.eof() && src2File.eof())
{
break;
}
}
src1File.close();
src2File.close();
destFile.close();
cout << "Done!.\n";
return 0;
}