C++ 在ios::in和out模式下打开文件时,fstream seekp()不工作

C++ 在ios::in和out模式下打开文件时,fstream seekp()不工作,c++,C++,我想替换二进制文件中的某些部分(中间)。如果我使用ostream out(“file.bin”,ios::binary),它将删除旧文件并创建新文件。但是如果我使用fstream out(“file.bin”,ios::binary | ios::in | ios::out),seekp()将不会转到正确的位置,并且tellp()始终返回-1。那么有没有办法替换文件中的某些部分呢 提前感谢。您必须打开流,并设置结尾、输入和输出位: std::fstream out("file.bin", ios

我想替换二进制文件中的某些部分(中间)。如果我使用
ostream out(“file.bin”,ios::binary)
,它将删除旧文件并创建新文件。但是如果我使用
fstream out(“file.bin”,ios::binary | ios::in | ios::out)
seekp()
将不会转到正确的位置,并且
tellp()
始终返回-1。那么有没有办法替换文件中的某些部分呢


提前感谢。

您必须打开流,并设置结尾、输入和输出位:

std::fstream out("file.bin", ios::binary | ios_base::ate);
这将防止在打开时重置文件;然后,使用<代码> SekpP和函数,您可以在中间编辑它。


此示例输出
stackovstrlow
,显示如何将所有步骤链接在一起:

#include <fstream>
#include <string>
#include <vector>
#include <iostream>

int main()
{
    // create the bin file
    {
        std::string str("stackoverflow\n");
        std::ofstream file("file.bin", std::ios_base::binary);
        file.write(str.c_str(), str.length() + 1);
    }
    // edit the bin file "in the middle"
    {
        std::fstream file("file.bin", std::ios_base::in | std::ios_base::out | std::ios_base::ate);
        file.seekp(7);
        file.write("str", 3);
    }
    // read and see what we've done
    std::ifstream file("file.bin", std::ios_base::binary);
    std::vector<char> v(14);
    file.read(v.data(), 14);
    std::string str(v.cbegin(), v.cend());
    std::cout << str;
}
#包括
#包括
#包括
#包括
int main()
{
//创建bin文件
{
std::string str(“stackoverflow\n”);
std::of流文件(“file.bin”,std::ios_base::binary);
file.write(str.c_str(),str.length()+1);
}
//编辑“中间”的bin文件
{
std::fstream文件(“file.bin”,std::ios_base::in | std::ios_base::out | std::ios_base::ate);
文件。见第7页;
写入(“str”,3);
}
//看看我们做了什么
std::ifstream文件(“file.bin”,std::ios\u base::binary);
std::向量v(14);
读取(v.data(),14);
std::string str(v.cbegin(),v.cend());

std::cout您必须打开流,并设置结尾、输入和输出位:

std::fstream out("file.bin", ios::binary | ios_base::ate);

这将阻止您的文件在打开时被重置;然后,使用<代码> SekpP和函数,您将能够在中间编辑它。


此示例输出
stackovstrlow
,显示如何将所有步骤链接在一起:

#include <fstream>
#include <string>
#include <vector>
#include <iostream>

int main()
{
    // create the bin file
    {
        std::string str("stackoverflow\n");
        std::ofstream file("file.bin", std::ios_base::binary);
        file.write(str.c_str(), str.length() + 1);
    }
    // edit the bin file "in the middle"
    {
        std::fstream file("file.bin", std::ios_base::in | std::ios_base::out | std::ios_base::ate);
        file.seekp(7);
        file.write("str", 3);
    }
    // read and see what we've done
    std::ifstream file("file.bin", std::ios_base::binary);
    std::vector<char> v(14);
    file.read(v.data(), 14);
    std::string str(v.cbegin(), v.cend());
    std::cout << str;
}
#包括
#包括
#包括
#包括
int main()
{
//创建bin文件
{
std::string str(“stackoverflow\n”);
std::of流文件(“file.bin”,std::ios_base::binary);
file.write(str.c_str(),str.length()+1);
}
//编辑“中间”的bin文件
{
std::fstream文件(“file.bin”,std::ios_base::in | std::ios_base::out | std::ios_base::ate);
文件。见第7页;
写入(“str”,3);
}
//看看我们做了什么
std::ifstream文件(“file.bin”,std::ios\u base::binary);
std::向量v(14);
读取(v.data(),14);
std::string str(v.cbegin(),v.cend());

std::cout对文件流的搜索应该是有效的,但并不总是有效的。值得注意的是,如果
imbue()
ed
std::locale()
使用的编码是可变宽度的,则搜索会失败。引用27.9.1.5[filebuf.virtuals]第13段:


效果:让
width
表示
a_codevt.encoding()
。如果
是_open()==false
,或者
off!=0&&width在文件流上搜索应该是有效的,尽管并不总是有效。值得注意的是,如果
imbue()
ed
std::locale()使用的编码,搜索会失败
是可变宽度。引用27.9.1.5[filebuf.virtuals]第13段:


效果:让
width
表示
a_codevt.encoding()
。如果
为_open()==false
,或
off!=0&&width C
open() API当然更清楚,“<代码> O-Tununc< /Cord>和WHOWNO..”:“JunZWikk我不是C++流的爱好者来编写二进制文件,我认为它们是用其他用例来构建的:)代码<> STD::IOSBASE::App/<代码>有助于替换“某个部分(中间)”不,仍然是错的。
app
意味着所有写操作都发生在最后。你可能是指
ate
?谢谢大家。
fstream out(“file.bin”,ios::binary,ios::in | ios::out | ios::ate)
终于成功了。C
open() API当然更清楚,“<代码> O-Tununc< /Cord>和WHOWNO..”:“JunZWikk我不是C++流的爱好者来编写二进制文件,我认为它们是用其他用例来构建的:)代码<> STD::IOSBASE::App/<代码>有助于替换“某个部分(中间)”?不,仍然错误。
app
表示所有写入操作都发生在末尾。您可能是指
ate
?谢谢大家。
fstream out(“file.bin”,ios::binary,ios::in | ios::out | ios::ate)
终于成功了。可以在文件中查找特定位置。您可能需要确保文件使用C语言环境(
std::locale::classic()
)在打开它之前:使用一些奇特的编码进行搜索将不起作用,并且使用
std::ios_base::binary
不会禁止代码转换。应该可以在文件中查找特定位置。您可能希望确保文件使用C语言环境(
std::locale::classic()
)不过,在打开它之前:使用一些奇特的编码进行搜索将不起作用,并且使用
std::ios_base::binary
不会禁止代码转换。