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


main173.cpp

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void main173()
{
	ofstream fout;
	ifstream fin;
	char filename[50];
	cout<<"Enter the file source:";//请输入当前目录下的文件,不可输入路径名,否则会出错
	cin>>filename;
	fin.open(filename);
	while(!fin.is_open())//打开失败
	{
		cout<<"\nWrong filename ,can't open it!";
		cout<<"Enter the file source:";
		cin>>filename;
		fin.open(filename);	
	}
	char copy[50]={"Copy"};
	strcat(copy,filename);//要写入的文件,因为我是在前面+Copy的
	fout.open(copy);
	while(!fout.is_open())//打开失败
	{
		cout<<"\nWrong Copyfilename ,can't open it!";	
		fout.open(copy);
	}
	string temp;
	while(!fin.fail())
	{
		getline(fin,temp);
		fout<<temp;
		fout<<endl;
	}
	if(fin.eof())
		cout<<"Copy complete!"<<endl;
	else
		cout<<"copy failed!"<<endl;

	fin.close();
	fout.close();

	cin.get();










}

 

 


发表回复

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