fstream读写创建可能不存在的文件
来源:互联网 发布:百视通网络电视电话 编辑:程序博客网 时间:2024/05/18 02:04
fstream file("b.txt", fstream::in|fstream::out|fstream::app);
ate 并不会导致create属性,
app可造成out属性
in 表示只读属性
out表可写属性+create shuxing
in+out 表只读+可写属性(没有创建属性)
dd.out.txt:
s
{
cout<<"---------------------------------------\n";
fstream file("dd.out.txt", fstream::out); // make dd.out.txt empty
//fstream file("dd.out.txt", fstream::out|fstream::in);
string s;
file>>s;
cout<<s;
file.close();
}
You're specifying std::fstream::in
in your call to fstream::open(). This is known to force it to require an existing file.
If you look at e.g. this reference, you will see:
app seek to the end of stream before each write
and
ate seek to the end of stream immediately after open
This means that ios::app
only writes at the end, but that ios::ate
reads and writes at the end by default. However, with ios::ate
you can seek freely in the file, but with ios::app
you will alwayswrite at the end, no matter what position you set for the writing pointer.
where filename is a string of characters representing the name of the file to be opened and mode is a combination of the following flags:void open (const char * filename, openmode mode);
ios::inOpen file for readingios::outOpen file for writingios::ateInitial position: end of fileios::appEvery output is appended at the end of fileios::truncIf the file already existed it is erasedios::binaryBinary modeThese flags can be combined using bitwise operator OR: |. For example, if we want to open the file "example.bin" in binary mode to add data we could do it by the following call to function-member open:
ofstream file;All of the member functions open of classes ofstream, ifstream and fstream include a default mode when opening files that varies from one to the other:
file.open ("example.bin", ios::out | ios::app | ios::binary);
classdefault mode to parameterofstreamios::out | ios::truncifstreamios::infstreamios::in | ios::out
//fstream file("b1.txt", fstream::in|fstream::out); do NOT create if no exist
fstream file("b1.txt", fstream::out); // create
file<<"s";
cout<<"s";
file.close();
}
{
fstream file("b.txt", fstream::in|fstream::out|fstream::app); // create
file<<"s";
cout<<"s";
file.close();
}
- fstream读写创建可能不存在的文件
- fstream库的文件读写
- C++ 创建、读写文件操作 fstream
- 使用fstream读写文件
- fstream读写文件
- C++ fstream读写文件
- 读写文件(fstream)
- fstream文件读写
- C++fstream读写文件
- C++ fstream读写文件
- 文件读写 fstream
- C++ fstream读写文件
- fstream读写UNICODE文件
- 用fstream创建文件
- 使用fstream创建文件
- fstream无法创建文件
- C++利用fstream读写文件
- C++ fstream文件读写操作
- Hacking之乐——揭秘带WIFI的SD卡
- android 悬浮窗口
- c语言字符串的计算方法
- MFC使用自定义资源加载PNG
- 从拼图游戏开始(一)_简单java模拟
- fstream读写创建可能不存在的文件
- IOS 手势与Button事件互不影响
- Android 根据网络分析运营商信息
- Objective-C编码规范和要求
- Win 7 字体突然改变
- ios 开发中获得当前时间的 年月日,时分秒
- linux 安装loadrunner loadgenerator详细过程
- Apache与tomcat时参数乱码
- 【数据结构】马踏棋盘的遍历问题