无法读取二进制文件

时间:2017-12-04 20:01:18

标签: c++ c++11

         // STRUCTURE FOR ADD NEW DRIVER'S RECORD 
        struct driver
       {
        int long reg;
        string name;
        string fhname;
        string peraddress;
        string pesentaddress;
        string dob;
        string cnic;
        string qualification;
        string occupation;
        string phone;
        string mobile;
        string licnum;
        string email;
        string city;
        string gender;
        string province;
        string lictype;
        string vitype;

    }e[100];


     // FUNCTION FOR ADD NEW RECORD OF DRIVER

      void add_driver_record()
      {
       system("CLS");
       char option;

       fstream driverTextFile, addrecord;
       addrecord.open("rcd.data", ios::out | ios::app | ios::binary);


       cout << "Do you Want to Enter New Record (Y/N) ";
       option = _getch();

       system("cls");


       if (option == 'Y' || option == 'y')
        {
          for (int i = 1; i == 1; i++)
           {
              while (option != 'N')
               {
                  cout << "\t\t Driver Information " << endl;
                  cout << "\t\t++++++++++++++++++++" << endl;

                  cout << "\t\tEnter Token No             :";
                  cin >> e[i].reg;

                  cout << "\t\tEnter License Number       :";
                  cin.ignore();
                  getline(cin, e[i].licnum);

                  cout << "\t\tEnter Full Name            :";
                  getline(cin, e[i].name);

                  cout << "\t\tEnter Father/Husband Name  :";
                  getline(cin, e[i].fhname);

                  cout << "\t\tEnter Permanent Address    :";
                  getline(cin, e[i].peraddress);

                  cout << "\t\tEnter Present Address      :";
                  getline(cin, e[i].pesentaddress);

                  cout << "\t\tEnter Date of Birth        :";
                  getline(cin, e[i].dob);

                  cout << "\t\tEnter CNI                  :";
                  getline(cin, e[i].cnic);

                  cout << "\t\tEnter city                 :";
                  getline(cin, e[i].city);

                  cout << "\t\tEnter Province             :";
                  getline(cin, e[i].province);

                  cout << "\t\tEnter Gender               :";
                  cin >> e[i].gender;

                  cout << "\t\tEnter Occupation           :";
                  cin >> e[i].occupation;

                  cout << "\t\tEnter Qaulification        :";
                  cin.ignore();
                  getline(cin, e[i].qualification);

                  cout << "\t\tEnter E-mail               :";
                  cin.ignore();
                  getline(cin, e[i].email);

                  cout << "\t\tEnter Phone No             :";
                  cin >> e[i].phone;

                  cout << "\t\tEnter Mobile No            :";
                  cin >> e[i].mobile;

                  cout << "\t\tEnter License Type         :";
                  cin.ignore();
                  getline(cin, e[i].lictype);

                  cout << "\t\tEnter Vehicle Type         :";
                  cin.ignore();
                  getline(cin, e[i].vitype);

                  addrecord.write((char *)&e, sizeof(driver));

                  driverTextFile <<                                                                                                   "========================================================\n";
                  driverTextFile << "\t\t   Driver Information  " << endl;
                  driverTextFile << "\t\t+++++++++++++++++++++++" << endl;
                  driverTextFile << "\t\t Token No              :" << e[i].reg << endl;
                  driverTextFile << "\t\t License Number        :" << e[i].licnum << endl;
                  driverTextFile << "\t\t Full Name             :" << e[i].name << endl;
                  driverTextFile << "\t\t Father/Husband Name   :" << e[i].fhname << endl;
                  driverTextFile << "\t\t Permanent Address     :" << e[i].peraddress << endl;
                  driverTextFile << "\t\t Present Address       :" << e[i].pesentaddress << endl;
                  driverTextFile << "\t\t Date of Birth         :" << e[i].dob << endl;
                  driverTextFile << "\t\t CNIC                  :" << e[i].cnic << endl;
                  driverTextFile << "\t\t City                  :" << e[i].city << endl;
                  driverTextFile << "\t\t Province              :" << e[i].province << endl;
                  driverTextFile << "\t\t Gender                :" << e[i].gender << endl;
                  driverTextFile << "\t\t Occupation            :" << e[i].occupation << endl;
                  driverTextFile << "\t\t Qaulification         :" << e[i].qualification << endl;
                  driverTextFile << "\t\t E-mail                :" << e[i].email << endl;
                  driverTextFile << "\t\t Phone No              :" << e[i].phone << endl;
                  driverTextFile << "\t\t Mobile No             :" << e[i].mobile << endl;
                  driverTextFile << "\t\t Enter License Type    :" << e[i].lictype << endl;
                  driverTextFile << "\t\t Enter Vehicle Type    :" << e[i].vitype << endl;
                  driverTextFile << "========================================================\n";

                  addrecord.close();

                  cout << "Do you Want to Enter New Record (Y/N) ";
                  cin >> option;

                  switch (option) {
                  case 'Y':
                  case 'y':
                   add_driver_record();
                   break;
                  case 'N':
                  case 'n':
                   exit(1);
                   break;
                  default:
                   printf("Please select suitable option \n");
                   add_driver_record();
                   break;
               }
              } // while Loop ends
             }// for loop ends
            } // if condition ends

           else if (option == 'N' || option == 'n')
           {
             printf("Please Select Suitable Option \n");
           }
        }


        // FUNCTION FOR DISPLAY ALL RECORDS OF DRIVER'S
         void display_all_driver()
         {

            system("CLS");
            system("color f5");

            fstream myfile, driverTextFile;
            myfile.open("rcd.data", ios::in | ios::out | ios::binary);

            int idx = 1;
            while (idx == 1)
             {

                myfile.read((char*)&e[idx].reg, sizeof(e[idx].reg));
                myfile.read((char*)&e[idx].licnum, sizeof(e[idx].licnum));
                myfile.read((char*)&e[idx].name, sizeof(e[idx].name));
                myfile.read((char*)&e[idx].fhname, sizeof(e[idx].fhname));
                myfile.read((char*)&e[idx].peraddress,              sizeof(e[idx].peraddress));
                myfile.read((char*)&e[idx].pesentaddress, sizeof(e[idx].pesentaddress));
                myfile.read((char*)&e[idx].dob, sizeof(e[idx].dob));
                myfile.read((char*)&e[idx].cnic, sizeof(e[idx].cnic));
                myfile.read((char*)&e[idx].city, sizeof(e[idx].city));
                myfile.read((char*)&e[idx].province, sizeof(e[idx].province));
                myfile.read((char*)&e[idx].gender, sizeof(e[idx].gender));
                myfile.read((char*)&e[idx].occupation, sizeof(e[idx].occupation));
                myfile.read((char*)&e[idx].qualification, sizeof(e[idx].qualification));
                myfile.read((char*)&e[idx].email, sizeof(e[idx].email));
                myfile.read((char*)&e[idx].phone, sizeof(e[idx].phone));
                myfile.read((char*)&e[idx].mobile, sizeof(e[idx].mobile));
                myfile.read((char*)&e[idx].lictype, sizeof(e[idx].lictype));
                myfile.read((char*)&e[idx].vitype, sizeof(e[idx].vitype));

                        cout << "========================================================\n";
                         cout << "\t\t   Driver Information  " << endl;
                         cout << "\t\t+++++++++++++++++++++++" << endl;
                         cout << "\t\t Token No              :" << e[idx].reg << endl;
                         cout << "\t\t License Number        :" << e[idx].licnum << endl;
                         cout << "\t\t Full Name             :" << e[idx].name << endl;
                         cout << "\t\t Father/Husband Name   :" << e[idx].fhname << endl;
                         cout << "\t\t Permanent Address     :" << e[idx].peraddress << endl;
                         cout << "\t\t Present Address       :" << e[idx].pesentaddress << endl;
                         cout << "\t\t Date of Birth         :" << e[idx].dob << endl;
                         cout << "\t\t CNIC                  :" << e[idx].cnic << endl;
                         cout << "\t\t City                  :" << e[idx].city << endl;
                         cout << "\t\t Province              :" << e[idx].province << endl;
                         cout << "\t\t Gender                :" << e[idx].gender << endl;
                         cout << "\t\t Occupation            :" << e[idx].occupation << endl;
                         cout << "\t\t Qaulification         :" << e[idx].qualification << endl;
                         cout << "\t\t E-mail                :" << e[idx].email << endl;
                         cout << "\t\t Phone No              :" << e[idx].phone << endl;
                         cout << "\t\t Mobile No             :" << e[idx].mobile << endl;
                         cout << "\t\t Enter License Type    :" << e[idx].lictype << endl;
                         cout << "\t\t Enter Vehicle Type    :" << e[idx].vitype << endl;
                         cout << "========================================================\n";

                         idx++;
                        }

                          myfile.close();



           }

如果其他人已经问过这个问题,我很抱歉。但我无法纠正我不能解决的问题。在控制台上读取二进制文件。让我告诉我的代码在做什么。我采用了一堆属性的结构并将输入输入并将该数据保存在二进制文件中。我想在第二种方法中读到它。但我完全无法读取数据。这是我的代码请给我解决方案。我已经尝试了几种方法,但我仍然无法做到。你的帮助将节省我的学期项目。

2 个答案:

答案 0 :(得分:4)

您的struct driverstd::strings组成,它通过指向动态分配内存的指针在内部表示实际内容。当您将这样的结构(或此类结构的数组)作为普通字节写入文件时,您实际上会写出&#34;内存地址&#34;而不是字符串的实际内容。因此,您将丢失实际内容,阅读文件将为您提供简单的垃圾。

尝试以下程序说明:

struct TestStruct {
    string testString;
};

int main(){
    TestStruct ts;
    cout << "sizeof ts with empty string: " << sizeof(ts) << endl;
    ts.testString = "Will not get larger, right?";
    cout << "sizeof ts with string set to some content: " << sizeof(ts) << endl;
}

输出:

sizeof ts with empty string: 24
sizeof ts with string set to some content: 24

答案 1 :(得分:-1)

您是否拥有该文件的读取权限?这是我第一次认为这是一个权限问题。