samedi 27 mars 2021

fileops.c no such file or directory while using fsream

I am trying to build a student management system. This is my code

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

fstream obj;

struct Student{
    string name,father,mother;
    int cls,roll;
    void GET(){
      cout<<"\nEnter Name:-"; cin.ignore(); getline(cin,name);
      cout<<"Enter Father:-"; cin.ignore(); getline(cin,father);
      cout<<"Enter Mother:-"; cin.ignore(); getline(cin,mother);
      cout<<"Enter Class:-"; cin>>cls;
      cout<<"Enter Roll:-"; cin>>roll;
    }
    void write(){ obj.write((char*)this,sizeof(*this)); }
    void Disp(){
      cout<<"\nName:-"<<name<<endl;
      cout<<"Father's Name:-"<<father<<endl;
      cout<<"Mother's name:-"<<mother<<endl;
      cout<<"Class:-"<<cls<<endl;
      cout<<"Roll:-"<<roll<<endl;
    }
};

void ReadFile();

int main(){
  try{
    int n; char ch; Student *ST;
    cout<<"What do you want?"<<endl<<"1. Enter data"<<endl<<"2. Read Data"<<endl;
    cin>>ch;
    switch(ch){
      case '1':{
        cout<<"Enter Number of Students="; cin>>n;
        if(n>0) obj.open("Student2.dat",ios::app|ios::binary);
        for(int i=0;i<n;i++){
          ST=new Student;
          ST->GET();
          ST->write();
          delete ST;
        }
        obj.close(); cout<<"Writing objects successfull";
        break;
      }
      case '2':{ ReadFile(); break; }
      default: { string error="Unlisted command not supported"; throw error; }
    }
  } catch(string err){
    cout<<endl<<err<<endl;
  }
  return 0;
}

void ReadFile(){
  int sum=0;
  ifstream ifobj("Student2.dat",ios::in|ios::binary);
  string error;
  Student x;
  if(ifobj){
    while(!ifobj.eof()){
      ifobj.read((char*)&x,sizeof(x));
      x.Disp();
    }
     ifobj.close();
  }
  else{
    error="Student data file not found";
    throw error;
  }
}

When I try to compile, it works fine. When I try to read my objects from the file, it gives me this error:-

Segmentation fault (core dumped)

Here's what I got by debugging and backtracing the a.out file by running the command gdb a.out core

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7c5b88c in _IO_new_file_xsputn (n=13, data=0x55d6945e68d0, f=<optimized out>) at fileops.c:1219
1219    fileops.c: No such file or directory.
(gdb) backtrace
#0  0x00007ffff7c5b88c in _IO_new_file_xsputn (n=13, data=0x55d6945e68d0, f=<optimized out>) at fileops.c:1219
#1  _IO_new_file_xsputn (f=0x7ffff7db56a0 <_IO_2_1_stdout_>, data=0x55d6945e68d0, n=13) at fileops.c:1197
#2  0x00007ffff7c4f541 in __GI__IO_fwrite (buf=0x55d6945e68d0, size=1, count=13, fp=0x7ffff7db56a0 <_IO_2_1_stdout_>) at libioP.h:948
#3  0x00007ffff7f09784 in std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) () from /lib/x86_64-linux-gnu/libstdc++.so.6
#4  0x0000555555556c8f in Student::Disp (this=0x7fffffffdc60) at Student.cpp:21
#5  0x0000555555556995 in ReadFile () at Student.cpp:79
#6  0x000055555555674f in main () at Student.cpp:63

I guess the main culprit is ifstream::read() function, because the fstream::write() function works fine. I have verified it by comparing the Student object size and the file size of Student2.dat.

It would be helpful if someone can tell me what's going on here. I am familiar with c++ but I have recently switched to Ubuntu

Aucun commentaire:

Enregistrer un commentaire