简体   繁体   中英

ifstream doesn't read wanted values

In an attempt to read a pcap's global file header, using the main code:

int main()
{
    std::ifstream in("test.pcap", std::ifstream::in | std::ifstream::binary);

    pcap_header gheader(in);


    in.close();

    return 0;
}

and the following gheader constructor:

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;
}

the returned output is:

0 ffcc 28 75b97220 38369fae fffffffe 75b8413a

instead of:

a1b2c3d4 2 4 0 0 40000 1

Looks like you're opening the file as binary: std::ifstream::binary but then reading like it's a text file. If it's a text file then remove std::ifstream::binary .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM