为什么在某些情况下不写入此文件?
Why isn't this file being written to under certain conditions?
所以我在这个网站上查看了不同的问题和答案,其中一个提到了一种测试程序是否运行过多次的方法。我决定尝试一下,但遇到了一个错误。
这应该是一个简单的测试程序,根据(最初为空的(文本文件check.txt
是否以x
开头来做两件不同的事情:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream file("check.txt", ios::in | ios::out);
if(!file){
cerr << "no file" << endl;
return 1;
}
char ch;
file >> ch;
if(ch == 'x'){
cout << "result one" << endl;
}
else {
cout << "result two" << endl;
file << 'x';
}
return 0;
}
我不太确定这里出了什么问题。
代码中有两个主要问题:
file.fail()
这是你应该检查的情况,不要!文件其次,使用<lt;操作员请阅读<lt;操作员:http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/.
相关文章:
- 为什么在某些情况下不写入此文件?
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 有没有办法在不使用 getline() 的情况下从.csv文件中读取?
- 是否可以在没有真实文件的情况下创建 ifstream
- 在 c++ 中连接字符串和整数,以便在 C++ 11 不支持计算机的情况下读取多个文件
- 如何在多写入器情况下对文件支持的共享内存中的大页面出错
- 如何在添加文件的情况下在VSCode中调试C++程序
- 每个文件(理想情况下是每个部分)的 clang 格式样式覆盖
- 如何在不重新创建现有文件的情况下写入.txt文件
- 如何在不知道大小的情况下读取文本文件并存储到数组中
- 如何使用Winforms C#或C++.Net在不更改文件中其他内容的情况下修改jpg文件中的Orientation e
- 如何在不循环的情况下使用getline读取文件
- 在不打开应用程序的情况下在MinGW c++中播放声音(.wav)文件
- C++-在没有自定义.lib文件的情况下从Lua C模块调用Lua函数
- 如何在不使用文件扩展名的情况下使用命令行参数打开C++中的文本文件?
- 如何在不受其他文件影响的情况下"by itself" Visual Studio 项目中运行C++文件?
- 如何在不包含完整的文件系统头的情况下使用文件系统的类路径C++17?
- 如何在不设置 ulimit -n 的情况下解决套接字程序打开太多文件的错误
- Nlohmann 在不知道密钥的情况下解析 JSON 文件
- C++:如何在不将命令传递给 shell 的情况下执行文件?