首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

新手,文件copy有关问题,请高手们指导

2012-02-07 
新手,文件copy问题,请高手们指导题目是:用户输入2个文件名,其中一个是存在数据的原文件,另一个则是要把原

新手,文件copy问题,请高手们指导
题目是:用户输入2个文件名,其中一个是存在数据的原文件,另一个则是要把原文件的内容复制到此文件。
当原文件不存在时,报错。如果outputfile不存在,则创建,并拷贝内容,存在,询问,是否覆盖,是,继续。否,结束

下面为原题:
?You need to ask the user for the names of the two files. 
?You need to check for all possible errors and give useful error messages when these occur. See perror() to assist with this. The program should terminate abnormally upon encountering an error. 
?If the output file exists already, consult the user as to whether they wish to over-write the file or not. If not, the program should terminate abnormally. 
?Include comments in your code to indicate all places where the code causes a system call to be made "behind the scene". 
?Functions you may use: ifstream, ofstream, peek, get, put, and so on


[解决办法]

C/C++ code
#include "iostream"#include "fstream"#include "string"#include "io.h"using namespace std;int main() {     char strName[128];        cout<<"Input File : ";    cin>>strName;    ifstream ifile(strName);    if (ifile.fail())    {        cout<<"error, can not open the input file!"<<endl;        return 1;    }    cout<<"Output File : ";    cin>>strName;    if (access(strName, 0) == 0)    {        cout<<"File Exists !  over-write or not(Y/N) : ";        string flag;        cin>>flag;        if (flag != "Y" && flag != "y")        {            ifile.close();            return 0;        }    }    ofstream ofile(strName);    ofile<<ifile.rdbuf();    ifile.close();    ifile.close();    return 0;} 

热点排行