输出文件里的内容的小问题。
代码如下:
#include <iostream>
#include<fstream>
using namespace std;
struct student
{
char name[10];
char sex;
int age;
char university[20];
};
int main()
{
student stu1;
ifstream ifs("1.txt");
if(ifs)
{
while( !ifs.eof())
{
ifs>>stu1.name>>stu1.sex>>stu1.age>>stu1.university;
cout<<stu1.name<<' '<<stu1.sex<<' '<<stu1.age<<' '<<stu1.university<<endl;
}
ifs.close();
}
else
cout<<"error! Can't open the file."<<endl;
cout<<endl;
return 0;
}
输出如下:kjjh m 232 kjfhe
fefasf w 34 kfjef
w 34
问题:1.txt文件中只有两行数据,没有最后一行的(w 34)。
不过在提醒他文件的末尾不是kfjef结尾的,而是多了一个空格。
请问:为什么在输出到控制台时多出了w 34 这一行?空格与这一行的关系,其中原理是什么?