用Python读取C++中的二进制文件

8ftvxx2r  于 2021-09-08  发布在  Java
关注(0)|答案(0)|浏览(343)

我想用C中的二进制模式读取文件。最初我使用python做同样的事情,当我使用python读取相同的文件时,我得到了结果 b'\xe0\xa4\xb5\xe0\xa4\xbe\xe0\xa4\xb9' 当转换为int时,会导致 224 164 181 224 164 190 224 164 185 我能注意到所有这些整数都在范围内 [0,255] .
我想在C
中做同样的事情,但是我也不想做同样的事情,我尝试了很多不同的技巧,但是我能得到的最好的结果就是C++给出了负数。


# include <iostream>

# include <io.h>

# include <fcntl.h>

# include <fstream>

# include <stdio.h>

int main()
{
    std::fstream file("a.txt", std::ios::out | std::ios::in | std::ios::binary);
    file.seekg(0, std::ios::end);
    int size = file.tellg();
    char ch;
    std::string text;
    std::cout << "Size = " << size << std::endl;
    file.seekg(0, std::ios::beg);
    char x;
    file.read((&x), 1);
    std::cout << static_cast<int>(x) << std::endl;
    return 0;
}

请忽略 #include 我用过很多。

OUTPUT

Size = 9
-32

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题