gpt4 book ai didi

c++ - 如何在 C++ 中找到文件指针位置?

转载 作者:行者123 更新时间:2023-11-28 05:20:23 26 4
gpt4 key购买 nike

我目前正在使用 C++ 处理文件,我想在某个位置之后读取文件。我在网上看到你不能打开一个文件来同时读取和写入。有没有办法在某个时刻返回文件指针的位置,并用它来提取它之后的信息?

最佳答案

你读错了。

#include <fstream>
#include <string>

int main()
{
std::fstream file("test.txt", std::ios::in | std::ios::out | std::ios::binary);

std::string s;

file >> s;

// get current read position
auto read_pos = file.tellg();

// set current write position
file.seekp(read_pos, std::ios::beg);
static const char data[] = "aaa";

// write some data
file.write(data, 3);
}

关于c++ - 如何在 C++ 中找到文件指针位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41651977/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com