lundi 4 décembre 2017

Can't able to read the binary file

         // 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();



           }

I am so sorry if somebody else had already asked this question. But I am unable to rectify the problem that I "can not" read the binary file on console. Let me tell what my code is doing. I took structure with bunch of attribute and taking the input in that and saving that data in Binary file. And i want to read that in second method. But I am completely unable to read the data. This is my code please provide me solution. I have already tried several ways but I am still unable. Your help will save me semester project.

Aucun commentaire:

Enregistrer un commentaire