c++ - 当我们在不打开文件的情况下写入文件时,内部会发生什么?
问题描述
例如,如果我写入一个文件,该文件不在我的 << 给定要写入的字符串所在的位置?
int main(int argc, char** argv) {
fstream my_file;
if (argc>1)
{
my_file.open("my_file.txt", ios::out);
cout << "File1 created successfully!"<<endl;
}
my_file << "Guru99";
my_file.close();
cout<<"Done"<<endl;
return 0; }
它会把它放在内存中还是放在 dev/null 类型的区域中?
解决方案
如果您尝试写入fstream
与文件无关(未打开)的对象,您的输出将被丢弃并failbit
设置:
如果无法生成输出,则设置失败位,如果在此流的异常掩码中启用了失败位上的异常,则抛出 ios_base::failure。
如https://en.cppreference.com/w/cpp/named_req/FormattedOutputFunction中所述。
推荐阅读
- android - 在 M1 Apple 芯片上使用 Android Studio 北极狐的 InvocationTargetException
- node.js - Mongoose 获取字段中每个唯一值的最新文档
- scala - 基于列值(measure_type)的具有枢轴和不同聚合的Spark Dataframe - Scala
- flask - 在 Flask 上分块 >100mb 文件
- flutter - Flutter 如何创建价格标签形状的盒子容器
- arduino - Arduino 中断按钮调用多个 ISR
- css - 如何制作 CSS 悬停下划线效果,其中线条是 Divi Wordpress 网站上主菜单项的箭头?
- android - Android Chrome 中的“文本下划线位置:下”不起作用
- android-studio - Android Studio Image Asset 工作室图标未居中
- php - 根据数组元素更改 SQL 查询