使用库更新文件<fstream>(仅覆盖特定部分)
Updating a file with <fstream> library (overwriting just a specific section)
我想问一下是否有任何方法可以更新内容为"ooooo"的文本文件"ooXXo"使用fstream库。我知道cstdio有一种方法,但我不想使用它,因为它不支持异常处理…此外,我不想将文件读取到内存,更新我想要的部分,然后将所有内容写入干净的文件…基本上,我正在寻找一种方法来做到这一点与fstream:
/* fseek example */
#include <stdio.h>
int main ()
{
FILE * pFile;
pFile = fopen ( "example.txt" , "w" );
fputs ( "This is an apple." , pFile );
fseek ( pFile , 9 , SEEK_SET );
fputs ( " sam" , pFile );
fclose ( pFile );
return 0;
}
#include <fstream>
int main()
{
std::ofstream os("example.txt");
os<<"This is an apple.";
os.seekp(9);
os<<" sam";
return 0;
}
#include <fstream>
int main (){
std::fstream pFile("example.txt");
pFile << "This is an apple.";
pFile.seekp(9);
pFile << "sam";
pFile.close();
return 0;
}
fstream默认打开文件进行读写。在构造函数中还有一个带默认值的参数,用来控制该行为。
相关文章:
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 使用ios:ate写入到流会覆盖现有文件
- 在基于范围的for循环中使用结构化绑定声明
- 使用 LuaBridge 将 LuaJIT 绑定到C++会导致"PANIC: unprotected error"
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- 我可以重新分配/覆盖std::字符串吗
- 在使用GPU支持编译Tensorflow时,会遇到CUDA_TOOLKIT_PATH未绑定变量
- 视觉studo 2019中的漫画和静态/动态绑定
- 将自由函数绑定为类成员函数
- 将常量指针引用绑定到非常量指针
- 叮叮当当在修复时插入多个"覆盖"说明符
- 谷歌模拟和覆盖关键字
- 如何将部分流作为参数传递
- 在派生类中绑定非静态模板化成员函数
- TMap::Emplace() 在应用现有密钥时会覆盖吗?
- 绑定派生类方法C++从实例范围之外的分隔 std::function 变量调用
- 在 openGL 中多次绑定缓冲区
- 最后一个参数将覆盖 SQLite 绑定中的先前参数
- 非覆盖虚拟函数的绑定类型
- 将c++的虚方法绑定到具有覆盖能力的js中