ios::app 不也暗示 ios::out 在C++
Does ios::app not also imply ios::out in C++?
我看到了另一个类似的问题,其中的答案是:
使用
app
意味着out
。该标准规定了应用程序和输出|应用程序 具有相同的结果,相当于模式"a"中的C fopen。
但就我而言,这似乎没有发生。我做了一个小代码来测试这一点。
#include<iostream>
#include<fstream>
#include<stdlib.h>
using namespace std;
int main()
{
fstream f;
char ch[10]={"Hello"};
f.open("Hello.txt",ios::app);
f<<ch;
if(f.fail())
{
cout<<"Failed to writen";
}
cout<<ch<<endl;
f.close();
system("pause");
}
这给了我输出:
Failed to write
Hello
此外,它没有向文件写入任何内容Hello.txt
这是否意味着ios::app
并不意味着ios::out
?或者,我犯了一个错误?
另外,我试图添加ios::out
#include<iostream>
#include<fstream>
#include<stdlib.h>
using namespace std;
int main()
{
fstream f;
char ch[10]={"Hello"};
f.open("Hello.txt",ios::app|ios::out);
f<<ch;
if(f.fail())
{
cout<<"Failed to writen";
}
cout<<ch<<endl;
f.close();
system("pause");
}
在这种尝试中,程序确实工作,它也写入文件。
虽然这对我来说很有说服力,ios::app
没有实现ios::out
但对这个问题的回答仍然让我怀疑,因为如果我们想要附加,那么我们当然想写入文件也是有道理的。
将 std::ios_base::out
和 std::ios_base::app
与 std::filebuf::open()
一起使用的行为(实际上,std::basic_filebuf<...>::open()
在表 132 中指定。与此讨论相关的三行是将std::ios_base::openmode
映射到与fopen()
一起使用的文件打开模式(这反过来定义了语义)的前三行。这些行定义以下映射:
-
std::ios_base::out
->"w"
-
std::ios_base::app
->"a"
-
std::ios_base::out | std::ios_base::app
->"a"
也就是说,除了std::ios_base::app
之外,是否还指定std::ios_base::out
并不重要。如果行为因存在std::ios_base::out
而异,则实现应该是错误的。
使用第一个旧的 c++ syle 代码在 Win7 + 资源管理器上的测试显示写入输出。但是同样的执行在记事本++下运行,带有folow脚本的NppExec显示了另一个结果:
npp_save
set GWDIR=G:MinGW471
cd "$(CURRENT_DIRECTORY)"
$(GWDIR)bing++ "$(FILE_NAME)" -o $(NAME_PART) -std=c++11 -march=native -O3
NPP_RUN $(NAME_PART)
如果我没记错的话,在旧的Windows资源管理器下,第一段代码显示了我们喜欢的内容。
这个 c++ 变体显示了我们在资源管理器和 NppExec 下期望的结果:
#include<iostream>
#include<fstream>
#include<stdlib.h>
using namespace std;
int main()
{
char ch[10]={"Hello"};
ofstream f("Hello.txt",ios::app);
f<<ch;
if(f.fail())
{
cout<<"Failed to writen";
}
cout<<ch<<endl;
system("pause");
}
请原谅我的英语。
- 使用ios:ate写入到流会覆盖现有文件
- 如何访问RTCVideoRenderer的帧?(ios)
- ios::fmtflags 在 C++ 中是如何工作的?setf() 是如何工作的?
- 我可以在 iOS 或 mac 应用程序中使用C++代码吗?
- "string.h"在构建适用于iOS的qt应用程序中找不到消息
- 为什么 C++ 11 在 ios 类中添加了运算符 bool
- 'string'文件在带有C++文件的iOS插件上找不到
- 文件模式标志"ios::app"是否用于删除文件(如果文件已存在)?
- 使用 C++ iOS::ate 获取错误的文件大小
- 使用 适用于 Android 和 iOS 的 tf-lite C++ API
- 如何正确设置 ios 标志以进行流操作?
- 在为 iOS 构建 aws-sdk-cpp 时,cmake 上的 CXX 编译器错误已损坏
- C++ std::locale( "en" ) 在 iOS 上引发异常
- 在瓦尔格林德的泄漏摘要中使用 std::ios::sync_with_stdio(fasle) 打印时获取"still reachable"
- 在iOS应用程序中创建数据库
- Qt & Firebase C++ SDK 在 iOS 上的链接问题
- 是否有可以在iOS上运行的API来更改现有视频的每秒帧数?
- 将文件从 iOS 应用程序加载到 C++ 对象中/<iostream>iOS 上的问题
- 将原始 IOS 相机数据上传到纹理
- 自动工具的正确 iOS 架构名称?