lanlinsong856 2024-08-19 21:45 采纳率: 60%
浏览 3

(标签-ios|关键词-stream)

有哪位大老
可以告诉我该怎么修改代码才不会多读最后的那个结点呢?

#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;
} 

img

img

img

  • 写回答

1条回答 默认 最新

  • 一直免费一直爽 2024-08-19 21:46
    关注

    以下回复参考:皆我百晓生券券喵儿等免费微信小程序作答:

    你可以在读取文件时检查是否已经到达文件末尾,以避免多读最后一个节点。你可以使用infile.eof()函数来检查是否到达文件末尾。以下是修改后的代码:

    #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()){ // 修改这里,使用!infile.eof()来判断是否到达文件末尾
            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;
    }
    

    这样修改后,程序在读取文件时就不会多读最后一个节点了。

    评论

报告相同问题?

问题事件

  • 修改了问题 8月19日
  • 创建了问题 8月19日

悬赏问题

  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图
  • ¥15 UE5.1局部变量对蓝图不可见
  • ¥15 一共有五道问题关于整数幂的运算还有房间号码 还有网络密码的解答?(语言-python)
  • ¥20 sentry如何捕获上传Android ndk 崩溃
  • ¥15 在做logistic回归模型限制性立方条图时候,不能出完整图的困难
  • ¥15 G0系列单片机HAL库中景园gc9307液晶驱动芯片无法使用硬件SPI+DMA驱动,如何解决?