一心只想AC 2023-06-09 14:08 采纳率: 74.6%
浏览 42
已结题

c++为什么从文件读数据读到的是空的?

下面这一段代码没法从文件中读入信息,看了好久也没发现哪里有问题。


int list::load(list* head) {

    list* rear = head;
    list* p = new list;

    ifstream ofile("d:\\stu.txt");
    if (!ofile) {
        cout << "打开文件失败" << endl;
        return 1;
    }
    
    for (;(ofile >> p->stu.ID >> p->stu.Name >> p->stu.Sex >> p->stu.IDC >> p->stu.Age >> p->stu.BP >> p->stu.Yx >> p->stu.Zy >> p->stu.Bj >> p->stu.Xz);) {
        rear->next = p;
        rear = p;
        rear->next = NULL;
        student::M += 1;
        list* p = new list;
    }
    cout << "文件读入成功" << endl;
    ofile.close();
}

以下是完整代码


#include<iostream>
using namespace std;
#include<stdio.h> 
#include<stdlib.h>
#include<string.h>
#include<fstream>

class student {
public:
    char ID[15];  //学号
    char Name[15];//姓名
    char Sex[3];  //性别
    char IDC[10]; //身份证号
    int  Age;     //年龄
    char BP[15];  //籍贯
    char Yx[30];  //院系
    char Zy[30];  //专业
    char Bj[30];  //班级
    char Xz[5];   //学制
    static int M;
};
class list {
public:
    student stu;
    list* next;

    void Insert(list *);//添加学生信息
    void Delete(list *);//删除学生信息
    void Modify(list *);//修改学生信息
    void Search(list *);//查找学生信息
    void  xsxx (list *);//输出所有学生信息
    int Save(list *);   //保存学生信息到文件
    int load(list *);//从文件中加载学生信息
};

void list::Insert(list* head) {
    list* rear = head;

    list* p = new list;

        cout<<"请输入学生的学号:";
        cin>>p->stu.ID;
        cout<<"请输入学生的姓名:";
        cin>>p->stu.Name;
        cout<<"请输入学生的性别:";
        cin>>p->stu.Sex;
        cout<<"请输入学生的身份证号:";
        cin>>p->stu.IDC;
        cout<<"请输入学生的年龄:";
        cin>>p->stu.Age;
        cout<<"请输入学生的籍贯:";
        cin>>p->stu.BP;
        cout<<"请输入学生的院系:";
        cin>>p->stu.Yx;
        cout<<"请输入学生的专业:";
        cin>>p->stu.Zy;
        cout<<"请输入学生的班级:";
        cin>>p->stu.Bj;
        cout<<"请输入学生的学制:";
        cin>>p->stu.Xz;
        cout<<"\n";
        for (; rear->next != NULL; rear = rear->next);

        rear->next = p;
        rear = p;
        rear->next = NULL;
        student::M += 1;

    cout<<"\n";
    cout<<"学生信息添加完毕!\n";
    cout<<"\n";
}
void list::Delete(list* head) {

    char c[15]; int i;
    int x = 0;

    cout<<"请输入要删除学生的学号:";
    cin>>c;
    list* pre = head;
    list* p;
    for (i = 0; i < student::M; i++) {
        p = pre->next;

        if (strcmp(p->stu.ID, c) == 0) {
            pre->next = p->next;

            x = 1;
            break;
        }
        pre = pre->next;
    }
    if (x == 0) { cout<<"该学号不存在\n"; return; }

    student::M--;
    cout<<"该学生信息已删除\n";
}
void list::Modify(list* head) {

        int i, x = 0, item = 1;
        char a[15];
        cout<<"请输入要修改的学生学号:";
        cin>>a;

        list* p, * pre;
        pre = head;

        for (i = 0; i < student::M; i++) {
            p = pre->next;
            if (strcmp(p->stu.ID, a) == 0) {//判断学号是否存在

                x = 1;
                cout<<"存在该学生,请输入您要进行的操作:\n";

                while (1) {
                    cout<<"1.修改姓名 ";
                    cout<<"2.修改性别 ";
                    cout<<"3.修改身份证号 ";
                    cout<<"4.修改年龄 ";
                    cout<<"5.修改籍贯 ";
                    cout<<"6.修改学院 ";
                    cout<<"7.修改专业 ";
                    cout<<"8.修改班级 ";
                    cout<<"9.修改学制 ";
                    cout<<"0.退出修改 ";
                    cout<<"\n";
                    cout<<"请输入您的操作:";
                    cin>>item;

                    switch (item) {

                    case 0:break;
                    case 1:cout<<"请输入新的名字:";
                        cin>>p->stu.Name; break;
                    case 2:cout<<"请输入新的性别:";
                        cin>>p->stu.Sex; break;
                    case 3:cout<<"请输入新的身份证号:";
                        cin>>p->stu.IDC; break;
                    case 4:cout<<"请输入新的年龄:";
                        cin>>p->stu.Age; break;
                    case 5:cout<<"请输入新的籍贯:";
                        cin>>p->stu.BP; break;
                    case 6:cout<<"请输入新的院系:";
                        cin>>p->stu.Yx; break;
                    case 7:cout<<"请输入新的专业:";
                        cin>>p->stu.Zy; break;
                    case 8:cout<<"请输入新的班级:";
                        cin>>p->stu.Bj; break;
                    case 9:cout<<"请输入新的学制:";
                        cin>>p->stu.Xz; break;
                    }
                    if (item == 0) break;
                    cout<<"修改成功\n";
                }

            }
            if (item == 0) break;
            pre = pre->next;
        }
        if (x != 1) cout<<"未找到该学生\n";

}
void list::Search(list* head) {

    int i, x1 = 0;
    char b[15];

    cout<<"请输入要查找的学生学号:";
    cin>>b;
    list* p, * pre;
    pre = head;
    for (i = 0; i < student::M; i++) {

        p = pre->next;
        if (strcmp(p->stu.ID, b) == 0) {
            x1 = 1;
            printf("学号:%s  姓名:%s  性别:%s  身份证号:%s  年龄:%d  籍贯:%s  学院:%s  专业:%s  班级:%s  学制:%s\n", p->stu.ID, p->stu.Name, p->stu.Sex, p->stu.IDC, p->stu.Age, p->stu.BP, p->stu.Yx, p->stu.Zy, p->stu.Bj, p->stu.Xz);
            cout<<"\n";
        }
        pre = pre->next;

    }if (x1 != 1) cout<<"未找到该学生\n";
}
void list::xsxx(list* head) {

    int i;

    cout<<"********************学生信息*********************\n";
    cout<<"\n";
    cout<<"学生总人数:%d\n", student::M;
    cout<<"学号  姓名  性别  身份证号  年龄  籍贯  院系  专业   班级   学制\n";
    list* pre;
    pre = head;
    for (i = 0; i < student::M; i++){
        pre = pre->next;
        printf(" %s    %s    %s   %s   %d   %s  %s  %s  %s  %s\n", pre->stu.ID, pre->stu.Name, pre->stu.Sex, pre->stu.IDC, pre->stu.Age, pre->stu.BP, pre->stu.Yx, pre->stu.Zy, pre->stu.Bj, pre->stu.Xz);
    }
}
int list::Save(list* head) {

    ofstream file("d:\\stu.txt");
    if (!file) {
        cout << "文件打开失败!" << endl;
        return 1;
    }
    list* pre;
    pre = head;
    
    for (int i = 0; i < student::M; i++) {
        pre = pre->next;
        file << pre->stu.ID << " " << pre->stu.Name << "" << pre->stu.Sex << " " << pre->stu.IDC << " " << pre->stu.Age << " " << pre->stu.BP << " " << pre->stu.Yx << " " << pre->stu.Zy << " " << pre->stu.Bj << " " << pre->stu.Xz << "\n";
    }
    cout << "文件保存成功" << endl;
    file.close();
}
int list::load(list* head) {

    list* rear = head;
    list* p = new list;

    ifstream ofile("d:\\stu.txt");
    if (!ofile) {
        cout << "打开文件失败" << endl;
        return 1;
    }
    
    for (;(ofile >> p->stu.ID >> p->stu.Name >> p->stu.Sex >> p->stu.IDC >> p->stu.Age >> p->stu.BP >> p->stu.Yx >> p->stu.Zy >> p->stu.Bj >> p->stu.Xz);) {
        rear->next = p;
        rear = p;
        rear->next = NULL;
        student::M += 1;
        list* p = new list;
    }
    cout << "文件读入成功" << endl;
    ofile.close();
}

void menu(list& p, list* head);//操作菜单

int student::M = 0;

int main(void) {
    list* head = new list;
    head->next = NULL;
    list s;
    char mm[10] = "123456";
    char m[10];
    int k = 0;
    while (1) {
        cout<<"请输入密码:";
        scanf_s("%s", m, 10);

        if (strcmp(m, mm) == 0) {
            k = 1;
            while (1) {

                menu(s,head);

                system("pause");

            }
        }
        else cout<<"密码错误\n";

        if (k == 1) break;
    }
    return 0;
}

void menu(list &s,list *head) {

    system("cls");//清空不必要的显示内容

    int num;

    cout<<"1.添加学生信息\n";
    cout<<"2.删除学生信息\n";
    cout<<"3.修改学生信息\n";
    cout<<"4.查找学生信息\n";
    cout<<"5.输出所有学生信息\n";
    cout<<"6.保存学生信息到文件\n";
    cout<<"7.从文件中读取学生信息\n";
    cout<<"8.退出系统\n";
    cout<<"--------------------------------------------------------------------------------------------------\n";
    cout<<"--------------------------------------------------------------------------------------------------\n";
    cout<<"请选择您的操作:";
    cin>>num;

    switch (num) {

    case 1:
        s.Insert(head);
        break;
    case 2:
        s.Delete(head);
        break;
    case 3:
        s.Modify(head);
        break;
    case 4:
        s.Search(head);
        break;
    case 5:
        s.xsxx(head);
        break;
    case 6:
        s.Save(head);
        break;
    case 7:
        s.load(head);
        break;
    case 8:
        cout<<"\n";
        cout<<"***********程序已退出***********";
        exit(0);
    default:cout<<"请输入正确的数字\n";
        cout<<"\n";
        break;
    }
}
  • 写回答

4条回答 默认 最新

  • 感谢地心引力 优质创作者: 嵌入式与硬件开发技术领域 2023-06-09 14:46
    关注
    
     
    int list::load(list* head) {
     
        list* rear = head;
        list* p = new list;
     
        ifstream ofile("d:\\stu.txt");
        if (!ofile) {
            cout << "打开文件失败" << endl;
            return 1;
        }
        
        for (;(ofile >> p->stu.ID >> p->stu.Name >> p->stu.Sex >> p->stu.IDC >> p->stu.Age >> p->stu.BP >> p->stu.Yx >> p->stu.Zy >> p->stu.Bj >> p->stu.Xz);) {
            rear->next = p;
            rear = p;
            rear->next = NULL;
            student::M += 1;
             p = new list;
        }
        cout << "文件读入成功" << endl;
        ofile.close();
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 6月17日
  • 已采纳回答 6月9日
  • 创建了问题 6月9日