我有以下代码:
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
string file;
cout << "Please enter input file name: ";
cin >> file;
int count[128] = {0};
ifstream in(file.c_str());
char one;
cin >> noskipws;
while(!in.eof()){
in.get(one);
count[static_cast<int>(one)]++;
cout << one << " : " << static_cast<int>(one) << endl;
}
}
输入文件:
Hello.
Line 2
我的程序告诉我文件末尾有两个2。为什么是这样?要清楚,这就是我输出的内容:
H : 72
e : 101
l : 108
l : 108
o : 111
. : 46
: 10
L : 76
i : 105
n : 110
e : 101
: 32
2 : 50
2 : 50
除了最后两个2之外,我想要的是什么。
答案 0 :(得分:1)
代码应该是这样的
while(in.get(one)){
count[static_cast<int>(one)]++;
cout << one << " : " << static_cast<int>(one) << endl;
}
它可以通过加倍结束号来解决您的问题。