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

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

我想用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++给出了负数。

  1. # include <iostream>
  2. # include <io.h>
  3. # include <fcntl.h>
  4. # include <fstream>
  5. # include <stdio.h>
  6. int main()
  7. {
  8. std::fstream file("a.txt", std::ios::out | std::ios::in | std::ios::binary);
  9. file.seekg(0, std::ios::end);
  10. int size = file.tellg();
  11. char ch;
  12. std::string text;
  13. std::cout << "Size = " << size << std::endl;
  14. file.seekg(0, std::ios::beg);
  15. char x;
  16. file.read((&x), 1);
  17. std::cout << static_cast<int>(x) << std::endl;
  18. return 0;
  19. }

请忽略 #include 我用过很多。

  1. OUTPUT
  2. Size = 9
  3. -32

暂无答案!

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

相关问题