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

关于文件读写

xishui777 发布于 2010-08-21 16:49, 541 次点击
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    ifstream file2("D:/test.txt");
    if(!file2)
    {
      cout<<"未打开读入文件!!!"<<endl;
      system("pause");
      return 0;
    }
    ofstream file3("D:/test2.txt");
    if(!file3)
    {
      cout<<"未打开写入文件!!!"<<endl;
      system("pause");
      return 0;
    }
    char a;
    while(file2.get(a))
      file3.put(a);
    file2.close();
    file3.close();
    system("pause");
    return 0;
}
代码为什么提示(未打开读入文件!!!)D:/test.txt文件已有
3 回复
#2
lintaoyn2010-08-21 17:24
"D:/test.txt"改成"D:\\test.txt"试下
#3
xishui7772010-08-21 17:45
改了,还是不行
#4
towhee2010-08-21 18:39
程序代码:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    ifstream file2("D:\\test.txt");
    if(!file2)
    {
        cout<<"未打开读入文件!!!"<<endl;
        system("pause");
        return 0;
    }
    ofstream file3("D:\\test2.txt");
    if(!file3)
    {
        cout<<"未打开写入文件!!!"<<endl;
        system("pause");
        return 0;
    }
    char a;
    while(file2.get(a))
        file3.put(a);
    file2.close();
    file3.close();
    system("pause");
    return 0;
}

以上代码编译通过,执行通过,环境vs2005+xp
1