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

来源:互联网 发布:前端开发优化问题 编辑:程序博客网 时间:2024/05/17 22:15

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;elsecout<<"copy failed!"<<endl;fin.close();fout.close();cin.get();}


0 0
原创粉丝点击