注册 登录
编程论坛 C++教室

c++文件拷贝问题

wu_qingzhou 发布于 2011-04-28 21:52, 530 次点击
//程序实现功能:将D盘里的两个文件(1.txt和2.txt)中的内容复制到D盘里的a.txt文档中
//问题:没有实现预订的功能,请问为什么?
#include <iostream>
#include <fstream>
using namespace std;

int main(void)
{    ofstream out;
    ifstream in;
    char ch;
    out.open("D:\\a.txt",ios::app);
    if (!out) { cout<<"out ERROR!"<<endl; }
    in.open("D:\\1.txt",ios::in);
    if (!in) { cout<<"1 ERROR!"<<endl; }
    while( in.get(ch) )    {    out<<ch;    }
    out<<ch;
    in.close();
    in.open("D:\\2.txt",ios::in);
    if (!in) { cout<<"2 ERROR!"<<endl; }
    while( in.get(ch) )    {    out<<ch;    }   
        out<<ch;
    in.close();
    out.close();
    return 0;
}
5 回复
#2
pangding2011-04-28 23:33
你运行着是什么結果?
#3
wu_qingzhou2011-04-29 13:35
回复 2楼 pangding
dos界面显示的是:
2 ERROR!
请按任意键继续...

D盘里的文件a.txt中只存放了1.txt中的内容,而没有存放2.txt里的内容。
#4
pangding2011-04-29 17:19
你在 2 之前加一个 in.clean() 之类的试试呢。
#5
玩出来的代码2011-04-29 20:22
读完1.txt流的状态就是错误的了,如楼上所说,in.clear()。、
#6
wu_qingzhou2011-04-30 10:31
回复 4楼 pangding
谢谢,你提供的方法通过了。谢谢
1