我想问是否有办法将内容为“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;
}
关于c++ - 使用 <fstream> 库更新文件(仅覆盖特定部分),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7590065/