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

关于文件读写

xishui777 发布于 2010-08-23 20:54, 476 次点击
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    char a[]="this is a book!!!";
    char k;
    fstream file2("F:/tsd.txt",ios::out|ios::in);
    if(!file2)
    {
      cout<<"不能打开此文件"<<endl;
      system("pause");
      return 0;
    }
    else
    file2<<a;
    while(!file2.eof())
    {
      file2.get(k);
      cout<<k;
    }
    file2.close();
    system("pause");
    return 0;
}
为什么不能在屏幕上显示啊?
还有个问题,如果F:\\tas.txt文件没有,为什么不能自己创建?
3 回复
#2
东海一鱼2010-08-23 21:40
fstream file2("F:/tsd.txt",ios::out|ios::in); //能创建,才奇怪。
#3
xishui7772010-08-23 22:14
fstream file2("F:/tsd.txt",ios::out|ios::in);
这种形式只能表示已有文件,是这样的吗?
#4
东海一鱼2010-08-23 22:41
是的。
fstream file2("d:\\tsd.txt",ios::out|ios::in|ios::app);//不存在,创建。存在,追加到文件后部。

1