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.

posted on 2011-12-14 14:09  justiner  阅读(770)  评论(0编辑  收藏  举报