ifstream读文件区别,.eof())多了好几个
ifstream读文件区别:
1.txt内容:
“a空格b空格c空格”
方式一:
int i=0;
ifstream readname(“1.txt”,ios::out);
while (!readname.eof())//检测到文件终止,直到无字符为止
{
readname>>ImageName
i++;
}
.eof读完c后还会继续读c之后的空格,故多出来了一次,最终i=4,当读c之后的空格后,ImageName仍为c.
方式二:
int i=0;
ifstream readname((“1.txt”,ios::out);
while (readname>>ImageName)
{i++;}
只读除了空格回车之外的字符,最终i=3.