简体   繁体   中英

unable to write to/read from file

The code I wrote seems to work when i write data in the file but when i look into the record.dat file after writing into it, it shows nothing. The segment with "Main Menu" gets repeated from the switch() every time i try to read from the file. I had to write the open commands with the access modes separately inside each case otherwise it wouldn't even open the file.

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <conio.h>

using namespace std;

class employee
{
private:
  int empcode;
  char empname[30];
  char empdesig[15];
  float empsalary;
public:
  void add_rec()
  {
    cout<<"\nEmployee Code : ";
    cin>>empcode;
    cout<<"\nEmployee Name : ";
    cin.ignore();
    cin.getline(empname,30);
    cout<<"\nEmployee Designation : ";
    cin.getline(empdesig,15);
    cout<<"\nEmployee Salary : ";
    cin>>empsalary;
  }
  void read_rec()
  {
    cout<<"\nEmployee Code : "<<empcode;
    cout<<"\nEmployee Name : "<<empname;
    cout<<"\nEmployee Designation : "<<empdesig;
    cout<<"\nEmployee Salary : "<<empsalary;
  }
};
int main()
{
  employee emp;
  fstream rfile;
  int ch,rec_no=0,pos=0;
  char ans='y',opt;
  do
  {
    cout<<"\n";
    cout<<"\t\t\t MAIN MENU ";
    cout<<"\n1. Add Record ";
    cout<<"\n2. Read Record ";
    cout<<"\n3. Modify Record ";
    cout<<"\n4. Exit "<<endl;
    cout<<"\nSelect an option : ";
    cin>>ch;
    switch (ch)
    {
      case 1:
            {
              rfile.open("record.dat", ios::out);
              char opt='y';
              cout<<"\t\t\tEmployee Data Entry "<<endl;
              do
              {
                emp.add_rec();
                rfile.write((char*)&emp,sizeof(emp));
                cout<<"\nEnter another record ? {Y/N} ";
                cin>>opt;
              } while(opt=='y'||opt=='Y');
            }
            break;
      case 2:
            {
              rfile.open("record.dat", ios::in);
              cout<<"\t\t\tEmployee Data Display "<<endl;
              rfile.read((char*)&emp,sizeof(emp));
              while(rfile)
              {
                emp.read_rec();
                rfile.read((char*)&emp,sizeof(emp));
              }
            }
            break;
      case 3:
            {
              rfile.open("record.dat", ios::out);
              cout<<"\t\t\tEmployee Data Modify "<<endl;
              cout<<"\nEnter the record no. to modify : ";
              cin>>rec_no;
              pos=(rec_no-1)*sizeof(emp);
              rfile.seekg(pos,ios::beg);
              rfile.read((char*)&emp,sizeof(emp));
              cout<<"\nModify this record ? {Y/N} "<<endl;
              cin>>opt;
              if(opt=='y'||opt=='Y')
              {
                cout<<"\n";
                cout<<"\t\t\tEnter New Data "<<endl;
                emp.add_rec();
                rfile.write((char*)&emp,sizeof(emp));
                cout<<"\nRecord Modified"<<endl;
                cout<<"\nPress any key to continue...";
                getch();
              }
            }
            break;
      case 4:
            break;
      default:
            cout<<"\nPlease select a valid option!";
            break;
    }
  } while(ch!=4);
  rfile.close();
  return(0);
}

Each time you open a file for writing under a case statement, you also need to close the file. When the file is closed, the double buffered values are then written to the physical file.

Under case 1, add a file close after the while statement: } while(opt=='y'||opt=='Y'); rfile.close();

Make similar updates to case 2 and 3, and both case 1 and 2 will now work. Case 3 has additional issues. For example, when opening the file to modify, you will want to use the "app" bit mask so you do not delete existing contents. See the following for more information: https://en.cppreference.com/w/cpp/io/basic_fstream/open

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM