无法在读取后写入具有入和出选项的文件流
Cant write after reading to file stream with in and out options
我的文件流有问题。我需要的是打开文件,读取它,然后在上面写一些值
std::string filename;
std::fstream save_file;
save_file.open(SAVED_HASH, std::fstream::in | std::fstream::out | std::fstream::binary);
while (save_file >> filename);
save_file.seekg(save_file.beg);
save_file << " more lorem ipsum";
save_file.close();
当我尝试运行它时,我得到的只是一个空文件。但如果我去掉第四行,它就行了。这个代码有什么问题,为什么我不能读取文件,然后写
我能发现的一个错误是之后
while (save_file >> filename);
您需要清除错误标志。
save_file.clear();
此外,IMO您需要使用seekp
而不是seekg
,因为您想在该位置"写入"。
有关示例,请参见此链接http://www.cplusplus.com/reference/ostream/ostream/seekp/?kw=seekp
相关文章:
- 如何传递多个 std::文件系统选项?
- 文件中.dat Dlib 选项
- Visual Studio C++ 它只构建选项卡中显示的文件吗?
- 打开 Docker 撰写文件中每个服务的外壳选项卡
- 在 ui 文件中隐藏 QTabWidget 中的单个选项卡窗格?
- 如何在Boost::program_options配置文件中为非字符串的自定义选项值类型处理空格
- 如何在 Boost Program_Options 中使用命令行和分层配置文件的选项
- 除了系统("pause")之外,是否有其他选项可以保持可执行文件打开?
- Emscripten - 在 CMakeList 文件中传递 emscripten 选项
- 我可以通过设置工具链文件来替换调用 cmake -G<generator> 选项和调用 vcvarsall.bat吗?
- C++ 使用 json 文件提升程序选项
- 如何从"开始组..结束组"链接器选项构建文件列表
- 使用 API 而不是全局配置文件设置 OSM 配置选项
- 是否有 G++ 7.2 选项在使用预编译头文件时发出消息?
- VS2017:如何在不破坏我设置的过滤器的情况下,以.vcxproj编写每个文件的选项?
- 制作文件后启用cmake选项/标志
- 如何将 -fix 选项添加到 .clang-tidy 文件
- 如何使用C/C++提取pcap文件的TCP标头中的选项字段
- 使用提升程序选项通过配置文件/命令行解析自定义对象
- 如何使用OS X api定位应用程序首选项文件夹