首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法写入/读取文件

问无法写入/读取文件
EN

Stack Overflow用户
提问于 2019-07-09 20:29:34
回答 1查看 80关注 0票数 0

当我在文件中写入数据时,我编写的代码似乎可以工作,但当我在写入record.dat文件后查看它时,它什么也没有显示。每次我尝试从文件中读取时,开关()中重复出现带有"Main Menu“的片段。我必须在每个案例中分别编写带有访问模式的open命令,否则它甚至不会打开文件。

代码语言:javascript
复制
#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);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-09 22:35:38

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

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56952624

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档