簡體   English   中英

ifstream不讀取想要的值

[英]ifstream doesn't read wanted values

為了嘗試使用主代碼讀取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

看起來您正在以二進制文件形式打開文件: std::ifstream::binary ,然后像文本文件一樣讀取文件。 如果是文本文件,則刪除std::ifstream::binary

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM