Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我对以下代码有疑问:
fstream s("tst.txt" , fstream::binary); s.seekp(5, fstream::beg); s.write("testing", 7);
它应该保持文件中的前五个字符不变,但事实并非如此!它丢弃了整个旧数据,我必须保留旧数据并只覆盖一个文件数据块,它应该是二进制模式,有人帮忙吗?提前致谢。
尝试这个:
fstream s("tst.txt" , fstream::binary | fstream::in | fstream::out); s.seekp(5, std::fstream::beg); s.write("testing", 7);
通过指定 openmode 标志,您将覆盖 fstream 构造函数调用中的默认 openmode。