C++ primer plus第六版课后编程题答案17.4


 

main174.cpp

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void main174()
{
	ifstream fin1("input1.txt");
	ifstream fin2("input2.txt");
	ofstream fout("output.txt");
	if(!fin1.is_open())
		cout<<" input1.txt open failure!"<<endl;
	if(!fin2.is_open())
		cout<<" input2.txt open failure!"<<endl;
	if(!fout.is_open())
		cout<<" output.txt open failure!"<<endl;

	string temp;
	while(fin1.good()&&fin2.good())
	{
		getline(fin1,temp);
		fout<<temp;
		fout<<" ";
		getline(fin2,temp);
		fout<<temp;
		fout<<endl;
	}
	if(fin1.eof()&&!fin2.eof())//1完了,2没完
	{
		while(fin2.good())
		{
		getline(fin2,temp);
		fout<<temp;
		fout<<endl;
		}
	}
	if(fin2.eof()&&!fin1.eof())
	{
		while(fin1.good())
		{
		getline(fin1,temp);
		fout<<temp;
		fout<<endl;
		}
	}
	if(fin1.eof()&&fin2.eof())
		cout<<"copy complete!"<<endl;
	else
		cout<<"something bad!"<<endl;
	fin1.close();
	fin2.close();
	fout.close();






}

 

 


发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注