Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ ifstream未读取文件中存在的'0a'字节_C++_File_File Io_Istream - Fatal编程技术网

C++ ifstream未读取文件中存在的'0a'字节

C++ ifstream未读取文件中存在的'0a'字节,c++,file,file-io,istream,C++,File,File Io,Istream,我想读取文件的前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]; f

我想读取文件的前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;;
}

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

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

info[i] = r.get();
get
读取单个字节而不跳过任何内容(除了可能的行尾转换,但您已经使用
ios::binary
)进行了计算)

r.read(信息,7)根据
info[i] = r.get();