无法写入/读取文件

时间:2019-07-09 12:29:33

标签: c++

当我在文件中写入数据时,我编写的代码似乎起作用,但是当我在写入到record.dat文件中后,它什么也没显示。每当我尝试从文件中读取时,带有“主菜单”的段都会从switch()中重复。在每种情况下,我必须分别使用访问模式编写打开命令,否则甚至都无法打开文件。

#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);
}

1 个答案:

答案 0 :(得分:0)

每次在case语句下打开要写入的文件时,也需要关闭该文件。关闭文件后,然后将双缓冲值写入物理文件。

在情况1下,在while语句后添加一个文件关闭: } while(opt =='y'|| opt =='Y'); rfile.close();

对情况2和3进行类似的更新,情况1和2现在都可以使用。情况3还有其他问题。例如,当打开文件进行修改时,您将要使用“ app”位掩码,这样就不会删除现有内容。有关更多信息,请参见以下内容: https://en.cppreference.com/w/cpp/io/basic_fstream/open