【发布时间】:2015-06-25 12:06:15
【问题描述】:
从文本文件中读取字符时,我不知道为什么最后一个字符会被读取两次?但是如果我在该行中插入一个新行,则它不再读取两次。
这是类
class ReadFromFile {
private:
std::ifstream fin;
std::string allMoves;
public:
ReadFromFile(std::string fileName) {
fin.open(fileName, std::ios::in);
char my_character;
if (fin) {
while (!fin.eof()) {
fin.get(my_character);
allMoves += my_character;
}
} else {
std::cout << "file does not exist!\n";
}
std::cout << allMoves << std::endl;
}
};
这里是文本文件的内容(没有换行符)
1,2 3,1 1,3 1,2 1,4
和输出:
1,2 3,1 1,3 1,2 1,44
【问题讨论】: