ifstream不读取想要的值

时间:2016-11-17 20:08:00

标签: c++ pcap

尝试使用主代码

读取pcap的全局文件头
int main()
{
    std::ifstream in("test.pcap", std::ifstream::in | std::ifstream::binary);

    pcap_header gheader(in);


    in.close();

    return 0;
}

和以下的gheader构造函数:

pcap_header::pcap_header(std::ifstream& in)
{
    in >> magic_number;
    in >> version_major;
    in >> version_minor;
    in >> thiszone;
    in >> sigfigs;
    in >> snaplen;
    in >> network;

    // debug
    std::cout << std::hex << magic_number << " " << version_major << " " << version_minor << " " << thiszone << " " << sigfigs << " " << snaplen<< " "  << network << std::endl;
}

返回的输出是:

0 ffcc 28 75b97220 38369fae fffffffe 75b8413a

而不是:

a1b2c3d4 2 4 0 0 40000 1

1 个答案:

答案 0 :(得分:2)

看起来你正在以二进制文件打开文件:std::ifstream::binary但是读取它就像是一个文本文件。如果是文本文件,请删除std::ifstream::binary