代码之家  ›  专栏  ›  技术社区  ›  Hadi

IFScript不读取文件中存在的“0A”字节

  •  0
  • Hadi  · 技术社区  · 7 年前

    我想读取文件的前7个字节。这是我的文件的数据:

    42 4d b6 fc 0a 00 00
    

    我用这个代码来做:

    #include <iostream>
    #include <fstream>
    #include <iomanip>
    
    using namespace std;
    
    
    int main()
    {
        ifstream r("TestFile.abc", ios::binary);
        unsigned char info[7];
        for(int i = 0 ; i < 7; i++){
            r >> info[i];
        }
    
        for(int i = 0 ; i < 7; i++){
          std::stringstream ss;
          ss << std::hex << (int) info[i]; // int decimal_value
          std::string res ( ss.str() );
          cout << i << setw(10) << info[i] << setw(10) << res << endl;
        }
    
    
    
       return 0;;
    }
    

    这是输出:

    0         B        42
    1         M        4d
    2         ╢        b6
    3         ⁿ        fc
    4                   0
    5                   0
    6                   0
    
    Process returned 0 (0x0)   execution time : 0.032 s
    Press any key to continue.
    

    为什么? 0a 字节已替换为 0 ?

    1 回复  |  直到 7 年前
        1
  •  2
  •   john    7 年前

    >> 跳过空白, 0a 是空白字符。试试这个

    info[i] = r.get();
    

    get 在不跳过任何内容的情况下读取单个字节(可能的行尾转换除外,但您已经使用 ios::binary )