[英]How to read any format of file to string for further compression with Huffman algorithm
如何讀取任何格式的文件(doc,pdf,jpeg)? 我的版本僅適用於txt,因此我無法正確解壓縮文件。
我從輸入文件中讀取的功能:
std::string getDataFromFileToString(std::string &fName)
{
std::string s;
std::ifstream fr(fName, std::ios_base::in | std::ios::binary);
if (!fr.is_open())
{
std::cerr << "File \"" << fName << "\" does not exist\n";
exit(EXIT_FAILURE);
}
char c;
while(fr.get(c))
s.push_back(c);
fr.close();
return s;
}
如果僅正確處理文本文件,則可能需要以二進制模式打開文件:
更改
std::ifstream fr(fName, std::ios_base::in);
至
std::ifstream fr(fName, std::ios_base::in | std::ios::binary);
並對輸出文件進行類似的更改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.