有哪位大老
可以告诉我该怎么修改代码才不会多读最后的那个结点呢?
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class staff{
private:
int num;
string name;
int age;
int wage;
public:
staff* next;
friend staff* get_from_file();
friend void print(staff* head);
};
staff* get_from_file(){
ifstream infile;
infile.open("C:\\Users\\10674\\Desktop\\f1.txt",ios::in);
staff* head,* p1,*p2;
head=NULL;
p1=p2=new staff;
int x=0;
while(infile.eof()==0){
infile>>p1->num>>p1->name>>p1->age>>p1->wage;
x++;
if(x==1){
head=p1;
}else{
p2->next=p1;
p2=p1;
}
p1=new staff;
}
p2->next=NULL;
return head;
}
void print(staff* head){
staff* p=head;
if(head==NULL){
cout<<"it is null"<<endl;
return;
}
while(p!=NULL){
cout<<"工号:";cout<<p->num<<endl;
cout<<"姓名:";cout<<p->name<<endl;
cout<<"年龄:";cout<<p->age<<endl;
cout<<"工资:";cout<<p->wage<<endl;
p=p->next;
}
}
int main(){
staff* head=get_from_file();
print(head);
return 0;
}