我正在学习C++,现在我已经创建了一个文件来进行一些加密/解密。在完成所有事情之后,我想知道文件压缩/解压的程度。所以我决定对输入和输出文件中的字符进行计数,但是这里出现错误。计数txt文件中的字符给出错误的计数
int get_compression(string file1, string file2){
string line = "";
ifstream stream1(file1.c_str());
double counter1 = 0.0;
while(getline(stream1, line)){
counter1 += line.length();
}
stream1.close();
cout << counter1 << "\n";
ifstream stream2(file2.c_str());
double counter2 = 0.0;
while(getline(stream2, line)){
counter2 += line.length();
}
stream2.close();
cout << counter2 << "\n";
return (counter2/counter1)*100;
}
我加入这两个cout
语句来看看它计数,但它告诉我,它已计数496个字符输入的txt文件,真正具有528个字符,并在txt文件481个字符有785个字符。我在某个地方犯了一些菜鸟错误吗?
'getline'丢弃结束行的字符从升压文档拍摄的例子。这可能是一些操作系统上的两个字符。为了可靠计数,使用'fstat'。 – usr2564301
@Jongware我会好起来的计数被两个字符关闭,但它与我的输出txt文件关闭了几百。 – Chronicle
您还需要以二进制模式打开文件,因为如果在文本模式下,您将获得一些自动翻译隐藏字节。 – RichardPlunkett