我正在尝试读取给定文件中的字符并输出十六进制字符的数量。当我对一个文本文件运行它时,它或多或少是准确的,但几乎没有其他任何东西,它似乎已经偏离了。IE:大约 700MB 的 *.mp4 文件将以 12K 出现。我在这里想念什么?
#include <fstream>
#include <iostream>
using namespace std ;
int main()
{
char letter ;
int i ;
cout << "Input the filename:" << endl;
string stringinput;
cin >> stringinput;
ifstream file( stringinput.c_str() ) ;
if( ! file )
{
cout << "Error opening input file, " << ( stringinput ) << ". Check file path and try again." << endl ;
return -1 ;
}
else
for( i = 0; ! file.eof() ; i++ )
{
file.get( letter ) ;
//cout << hex << (int) letter;
}
cout << endl;
float k = 1024, m = 1048576;
file.close();
if( i < 1024)
{
cout << "Total: " << dec << i << endl;
}
else if( i < m)
{
cout << "Total: " << dec << (i / k) << "K" << endl;
}
else
{
cout << "Total: " << dec << (i / m) << "M" << endl;
}
return 0 ;
}