通过声明fstream类对象来打开一个流文件有什么有关问题
通过声明fstream类对象来打开一个流文件有什么问题?
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream ou("E:\\120701.txt", ios::in | ios::out);
if(!ou)
{
cout<<"file open fail!\n";
system("pause");
exit(1);
}
cout<<"open the file!\n";
system("pause");
return 0;
}
为什么不能打开文件?写成:
fstream ou("E:\\120701.txt", ios::in | ios::out | ios::app);
或
fstream ou("E:\\120701.txt", ios::in | ios::out | ios::app);
可以?
原来E盘根目录下不存在120701.txt文件。为什么程序不会默认创建一个新文件?
------最佳解决方案--------------------
与fopen一致的吧
r或者r+模式要求文件必须存在
ios::in
------其他解决方案--------------------
貌似文本打开方式有2种,一种是二进制流,一种文本方式,用fstream应该只能是二进制方式打法。
------其他解决方案--------------------
ios::out 就相当于 r+
------其他解决方案--------------------
掩码
------其他解决方案--------------------
完全错误!
------其他解决方案--------------------
fstream out;
out.open(.....);一般我这么写
------其他解决方案--------------------
哦,我知道了。用fstream()函数来声明一个标准输入输出流对象来关联一个磁盘文件时,如果此文件不存在,它不会自动创建一个新文件来进行I/O操作。这也符合逻辑,当此文件根本就不存在,那怎么能试图打开此文件来进行输出操作呢,不知道我有没有理解错,欢迎板砖
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream ou("E:\\120701.txt", ios::in | ios::out);
if(!ou)
{
cout<<"file open fail!\n";
system("pause");
exit(1);
}
cout<<"open the file!\n";
system("pause");
return 0;
}
为什么不能打开文件?写成:
fstream ou("E:\\120701.txt", ios::in | ios::out | ios::app);
或
fstream ou("E:\\120701.txt", ios::in | ios::out | ios::app);
可以?
原来E盘根目录下不存在120701.txt文件。为什么程序不会默认创建一个新文件?
------最佳解决方案--------------------
与fopen一致的吧
r或者r+模式要求文件必须存在
ios::in
------其他解决方案--------------------
貌似文本打开方式有2种,一种是二进制流,一种文本方式,用fstream应该只能是二进制方式打法。
------其他解决方案--------------------
ios::out 就相当于 r+
------其他解决方案--------------------
static const _Openmode in = (_Openmode)0x01;
static const _Openmode out = (_Openmode)0x02;
static const _Openmode ate = (_Openmode)0x04;
static const _Openmode app = (_Openmode)0x08;
static const _Openmode trunc = (_Openmode)0x10;
static const _Openmode _Nocreate = (_Openmode)_IOS_Nocreate;
static const _Openmode _Noreplace = (_Openmode)_IOS_Noreplace;
static const _Openmode binary = (_Openmode)_IOSbinary;
掩码
------其他解决方案--------------------
完全错误!
------其他解决方案--------------------
fstream out;
out.open(.....);一般我这么写
------其他解决方案--------------------
哦,我知道了。用fstream()函数来声明一个标准输入输出流对象来关联一个磁盘文件时,如果此文件不存在,它不会自动创建一个新文件来进行I/O操作。这也符合逻辑,当此文件根本就不存在,那怎么能试图打开此文件来进行输出操作呢,不知道我有没有理解错,欢迎板砖