【问题标题】:exception error in reading from file using c++ file handling使用 C++ 文件处理从文件中读取异常错误
【发布时间】:2018-03-17 06:40:39
【问题描述】:

该程序将字符串(学生姓名)作为输入,根据文件中已存在的学生人数生成该学生的卷号。然后将此信息作为结构存储在该文件中。更多详细信息在 cmets 中。此代码运行良好,直到我更改其中任何一个 ->

  1. 在 main() 函数中,如果我发送 'data stud' 作为参考,它工作正常。但是,如果我将它作为副本发送,这意味着每次都在文件中创建一个新的类对象用于写入和读取(这很好,不是吗?)。它给出了一个例外。
  2. 在 rollGen() 函数中,我创建了一个班级数据对象 temp 来计算该文件中已经存在数据的学生人数。如果此对象是动态创建的,则代码可以正常工作。但是如果我静态地创建它,程序就会给出一个异常(例如参见 cmets)。 我想知道,为什么会发生这种情况是与范围相关的事情......还是其他什么。提前致谢。例外 -

    #include<iostream>
    #include<string>
    #include<fstream>

    struct student
    {
        char RollNo[8];
        std::string name;
    };

    class data
    {
        student stu;
    public:
        data() {}
        //open a file for fstream in argument...mod = 0 for input mode...mod = 1 for output mode
        void openn(int mod, std::fstream* yo_fp)    
        {
            if (mod == 0)
            {
                yo_fp->open("cs.txt", std::ios::in);
            }
            else if (mod == 1)
            {
                yo_fp->open("cs.txt", std::ios::out | std::ios::app);
            }
        }
        //generates roll number of student 
        //for example if data of 3 students is already present in file then roll number of new student will be cs4
        void rollGen()
        {
            std::fstream* rg_fp = new std::fstream;
            openn(0, rg_fp);
            int i = 1;
            data* temp = new data;  
            //if I allocate temp on stack...like, 'data temp;' and then in while loop replace 'temp' with '&temp'...programm is giving exception
            //counting number of students whose data is already present in file
            while (rg_fp->read((char*)temp, sizeof(data)))
            {
                i++;
            }
            strcpy_s(stu.RollNo, "cs"); 
            std::string s = std::to_string(i);
            strcat_s(stu.RollNo, s.c_str());
            rg_fp->close();
            delete rg_fp;
        }
        //take name string as input and call roll number generation function. hence providing data to student stu struct present in this class
        void input()
        {
            std::cout << "Enter your name - ";
            getline(std::cin, stu.name);
            rollGen();
            std::cout << "your RollNo is - " << stu.RollNo << std::endl;
        }
        //printing tha data of student stu struct present in class
        void output()
       {
            std::cout << "roll no - " << stu.RollNo << std::endl;
            std::cout << "name - " << stu.name << std::endl;
        }
    };
    //write the data of class object into file after calling input() function
    //using 'data stud' in argument instead of 'data& stud' gives exception
    void write(data& stud, std::fstream* cur_fp)
    {
        stud.input();
        stud.openn(1, cur_fp);
        cur_fp->write((char*)&stud, sizeof(data));
        std::cout << std::endl;
        cur_fp->close();
    }
    //printing all elements of file one by one
    //using 'data stud' in argument instead of 'data& stud' gives exception
    void read(data& stud, std::fstream* cur_fp)
    {
        stud.openn(0, cur_fp);
        std::cout << "Data in File is - " << std::endl << std::endl;;
        while (cur_fp->read((char*)&stud, sizeof(data)))
        {
            stud.output();
            std::cout << std::endl;
        }
        cur_fp->close();
        std::cin.get();
    }

    int main()
    {
        std::fstream *fp = new std::fstream;
        data stud;
        write(stud, fp);
        write(stud, fp);
        read(stud, fp);
        delete fp;
    }

【问题讨论】:

  • 您使用多长的名称?您输入的名称是否过长,导致字符数组 RollNo 的末尾被覆盖?
  • @ErikAlapää 我使用的是 3 个字符长的名称,没有空格。
  • 您在该代码中存在多个问题,我可以快速查看这些问题 - 尤其是它无法编译,更不用说抛出异常了,因为 strcpy_s()strcat_s() 是用两个参数调用,实际上需要三个。由于代码中的函数交互很多,而且您不必要地使用了指针,因此存在很大的问题空间。您需要努力减少无关代码的数量,并隔离问题(或问题),否则没有其他人有机会帮助您。投票结束。

标签: c++ exception file-handling


【解决方案1】:

data 包含一个成员字段sudent stu 包含一个成员字段std::string name,它将内容存储在堆上,因此通过从文件rg_fp-&gt;read((char*)temp, sizeof(data)) 中读取data 对象,您基本上用无效指针填充字符串,这些指针指向所有各种未定义的行为。请注意,将其写入cur_fp-&gt;write((char*)&amp;stud, sizeof(data)); 之类的文件也不起作用,因为未写入字符串内容。您应该实现正确的 [de] 序列化存储每个类字段的内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    • 2014-05-26
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 1970-01-01
    • 2014-08-26
    相关资源
    最近更新 更多