相关疑难解决方法(0)

fstream seekg(),seekp()和write()

我正在寻找关于你何时写文件的方式seekg()seekp()工作的一些说明.比方说我有一个像这样的文件:

offset 0: 2
offset 4: 4
offset 8: 6
offset 12: 8
offset 16: 10
Run Code Online (Sandbox Code Playgroud)

现在我想打开文件并尝试读取和写入值.

fstream file;
file.open("file.txt", fstream::in |fstream::out | fstream::binary);
file.seekp(0, ios::end) // seek to the end of the file
int eofOffset = file.tellp(); // store the offset of the end-of-file, in this case 20

int key = 0;

file.seekg(12, ios::beg); // set the seek cursor to offset 12 from the beginning of the file
file.read((char *) &key, (int)sizeof(int)); // read in …
Run Code Online (Sandbox Code Playgroud)

c++ file-io fstream seekg

21
推荐指数
1
解决办法
6万
查看次数

标签 统计

c++ ×1

file-io ×1

fstream ×1

seekg ×1