【问题标题】:why my program does not read the data from file为什么我的程序不从文件中读取数据
【发布时间】:2015-07-17 14:45:55
【问题描述】:

当我按“2”读取文件时,虽然我的语法正确,但无法读取文件中的数据。为什么我的程序无法读取数据?顺序访问文件和随机访问文件有什么区别,为什么首选随机访问文件?

void EnterRecord();
void ShowRecord();
using namespace std;
class student
{
    int rollno;
    string name;
    int marks;
    public:
        void SetData();
        void DisplayData();
        void MainMenu();
};
   void student:: MainMenu()
   {
      cout<<"                             "<<endl;
      cout<<"press 1 to enter record"<<endl;
      cout<<"press 2 to show record"<<endl;
   }
   void student::SetData()
   {
      cout<<"enter roll no of student"<<endl;
      cin>>rollno;
      cout<<"enter name of student"<<endl;
      cin>>name;
      cout<<"enter marks of student"<<endl;
      cin>>marks;
   }
   void student::DisplayData()
   {
      cout<<rollno<<setw(10)<<setw(10)<<marks<<endl;
   }

     int main()
     {
            student s1;
        int choice;
        repeat:
        s1.MainMenu();
        cout<<"enter ur choice ::"<<endl;
        cin>>choice;

        switch(choice)
        {
            case 1:
                EnterRecord();
                break;
                case 2:
                    ShowRecord();
                    break;
         }
           return 0;
     }

      void  EnterRecord()
   {
      ofstream myfile;
      myfile.open("student3.txt",ios::app);
      int choice=1;
      while(choice==1)
      {
        student s1;
          s1.SetData();

          myfile.write((char*)&s1,sizeof(student));
          cout<<"press 1 to enter record again"<<endl;
          cin>>choice;
          if(choice!=1)
          {
            system("cls");
           s1.MainMenu();
      }
         }
           myfile.close();
   }
     void ShowRecord()
     {
        student s2;
         ifstream myfile;
         myfile.open("student3.txt");
         myfile.seekg(0);
         while(myfile.eof())
         {
            myfile.read((char*)&s2,sizeof(student));
          }
          s2.DisplayData();
     }

【问题讨论】:

  • 您的case 表达式不正确。它们应该是'1''2',而不是简单的12。您还应该开始检查您调用的函数返回的错误。另见Why is iostream::eof inside a loop condition considered wrong?
  • 请不要随便在句末加上完全不同的问题...
  • 寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及重现它所需的最短代码在问题本身。没有明确问题陈述的问题对其他读者没有用

标签: c++ file-handling


【解决方案1】:

最大的问题是您将student 的实例视为单个普通旧数据(POD)块,而实际上它不是。它包含一个std::string 的实例,其中包含您不知道的内部数据。第一个也是最明显的是它包含一个指向字符串数据的指针。当你把它写出来时,你保存的是指针值而不是实际的字符串。当你重新加载它时,它会读取 old 指针值,该值只有在你是一个非常幸运的小妖精时才有效。

您的函数EnterRecordShowRecord 将需要更新,以便它们序列化student 的每个成员,而不是将其写成一个普通的旧数据块。下面的内容应该可以帮助您入门。

保存数据:

myfile << st.rollno;
myfile << st.name;
myfile << st.marks;

加载数据

myfile >> st.rollno;
myfile >> st.name;
myfile >> st.marks;

由于您没有提供有关文件布局方式的任何信息,因此您可以自行决定如何布局。传统上你会超载operator&gt;&gt;operator&lt;&lt;,但因为这看起来像是家庭作业,可能有点多。

您的代码还有其他几个问题...

  1. 您的 case 表达式不正确。您正在使用 values 1 和 2,它们与 12 键的 ASCII 值不同。您应该使用 '1' 和 '2' 而不是简单的 1 和 2。

  2. 您还应该开始检查您调用的函数返回的错误,例如open

  3. 在你的 while 循环中检查 eof 肯定是行不通的。见问答@Why is iostream::eof inside a loop condition considered wrong?

【讨论】:

    【解决方案2】:

    应该是:

    switch(choice)
    {
        case '1':
            EnterRecord();
            break;
        case '2':
            ShowRecord();
            break;
    }
    

    【讨论】:

    • 这不是唯一的问题。在while 循环中使用eof 将无法正常工作,并且myfile.read((char*)&amp;s2,sizeof(student)); 完全完全错误
    • switch 语句正在工作,问题在于从文件中读取数据。
    • @jawad 不,不是。文字 '1' 的值与 1 (在 case 表达式中使用)not 相同。所以要么它不工作,要么你没有发布你的真实代码。
    • @CaptainObvlious 你能建议我的语法吗
    • @CaptainObvlious 这个 myfile.read((char*)&s2,sizeof(student)); 有什么问题?你能解释一下吗
    猜你喜欢
    • 1970-01-01
    • 2022-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多