我寫一個結構到一個文件中使用以下行讀取二進制數據從二進制文件
typedef struct _rawData rawDataStruct;
現在寫的結構進入後文件,我正在閱讀從二進制文件開始使用的結構:
std::cout << "going for print data read from file\n";
snif::fileHandler.seekg(0); //, std::ios::beg);
snif::fileHandler.read(reinterpret_cast<char*>(rawData), sizeof(rawDataStruct));
if (snif::fileHandler.fail()) {
std::cerr << "reading error\n";
exit(0);
}
std::cout << "PSH flag = " << rawData->tcpFlag.PSH << std::endl
<< "source port " << rawData->sourcePort << std::endl
<< "destination port " << rawData->destinationPort << std::endl
<< " sequence number " << rawData->sequenceNumber << std::endl
<< " Acknowledge number " << rawData->acknowledgeNumber << std::endl
<< " acknowledge flag " << rawData->tcpFlag.ACK << std::endl
<< " SYN flag " << rawData->tcpFlag.SYN << std::endl
<< "FIN flag " << rawData->tcpFlag.FIN << std::endl;
但如果我檢查我的標準輸出,歌廳打印的最後一行是:
"going for print data read from file";
您是否在讀取之前檢查過'snif :: fileHandler'是否可以讀取(使用例如'.fail()')?如果打開失敗,我猜你的程序正試圖從標準輸入讀取(文件描述符0)。 – Thomas