2015-11-08 128 views
-2

這是我的* .h反序列化類實例++

class ip_file_handler { 

    struct IPheader { 
     unsigned short int ver :4; 
     unsigned short int ihl :4; 
     unsigned short int total_length; 
     unsigned char tos; 
     unsigned char ttl; 
     unsigned char protocol; 
     unsigned int checksum; 
     unsigned short int identification; 
     unsigned short int flag_offset; 
     unsigned int ucSource; 
     unsigned int ucDestination; 
    }; 

    public: 

    ip_file_handler(){} 

    // func 1 (file to ip) 
    static bool file_to_ip_packets(std::string input_file_name, 
            std::string output_file_name, 
            unsigned int source_ip, 
            unsigned int destination_ip); 

    private: 

    IPheader ipInfo; 
    list<string> data; 
}; 

在*的.cpp

文件的所有數據導入將myHandler。

此行寫類文件:

ofstream outFile(output_file_name.c_str(), ios::out | ios::binary); 
ip_file_handler myHandler; 
outFile.write((char*) &myHandler,sizeof(ip_file_handler)); 

/*這裏的問題*/ 這條線讀取類從文件:

ip_file_handler tmp; 
outFile2.read((char*) &tmp, sizeof(ip_file_handler)); 

現在爲什麼不能從文件中讀取類! ? outFile is'nt empty!

謝謝大家。 Adam

+0

通常,將對象二進制轉儲到文件並將二進制轉儲回內存是不安全的。您需要添加函數(或流操作符)來手動編寫/讀取每個成員。 –

+2

正確設置您的問題並使用正確的拼寫和語法。如果你沒有在你的問題上付出任何努力,不要指望陌生人關心你的問題。 –

回答

1

列表中的數據(eh,data)未存儲在您的班級內,而是單獨分配到堆上。當您只需轉儲ip_file_handler的副本時,它不會存儲在文件中。

您將不得不單獨寫出列表中的每個字符串,並在再次讀取列表時重新創建列表。

+0

謝謝波佩爾森,我會試試這個。 –

相關問題