Home > Net >  issue while writing and reading array of objects C
issue while writing and reading array of objects C

Time:05-29

facing an issue while writing and reading array of objects, when I put and write data of array object [0] and object 1, it puts object [0] data in object 1 as well, I think their many other issues with code, if anyone can guide me, I would be really grateful.

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

class A
{
    private:
        int num;
    public:
        void putdata(){
            cout<<"Enter Num: ";
            cin>>num;
            this->num=num;
        }
        void getdata(){
            cout<<"The num is : " << num;
        }
        void ReadFunc(A[],int i);
        void WriteFunc(A[], int i);
};
//
void A :: ReadFunc(A ob1[],int i){
    ifstream R;
    R.open("File9.txt", ios::in | ios :: binary);
    cout<<"\nReading the object from a file : \n";
    R.read( (char *) & ob1[i], sizeof(ob1[i]));
    ob1[i].getdata();
    R.close();
}
void A :: WriteFunc(A ob1[],int i){
    ofstream W;
    W.open("File9.txt", ios::out | ios::app );
    ob1[i].putdata();
    W.write( (char *) & ob1[i], sizeof(ob1[i]));
    W.close();
    cout<<"Congrats! Your object is successfully written to the file \n";
}

int main()
{
    A ob1[100];
      ob1[0].WriteFunc(ob1,0);
      ob1[1].WriteFunc(ob1,1);
      cout<<"\n";
      ob1[0].ReadFunc(ob1,0);
      ob1[1].ReadFunc(ob1,1);
    
    return 0;
}

Output

CodePudding user response:

Rough implementation of index plus object.

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

class A
{
    private:
        int num;
    public:
        void putdata(){
            cout<<"Enter Num: ";
            cin>>num;
            this->num=num;
        }
        void getdata(){
            cout<<"The num is : " << num;
        }
        void ReadFunc(A[],int i);
        void WriteFunc(A[], int i);
};
struct A_withIndex { size_t index; A data; };

void A :: ReadFunc(A ob1[],size_t i){
    ifstream R;
    R.open("File9.txt", ios::in | ios :: binary);
    A_withIndex buffer;
    bool found=false;
    while(R){
        R.read( (char *) &buffer, sizeof(buffer));
        if(R.gcount()!=sizeof(buffer)) break;
        if(buffer.index==i){
            found =true;
            obj[i]=buffer.data;
        }
    }
    if(found){
        cout<<"\nReading the object from a file : \n";
        ob1[i].getdata();
    }
    else{
        cout<<"\nObject not found \n";
    }
    R.close();
}

void A :: WriteFunc(A ob1[],size_t i){
    ofstream W;
    W.open("File9.txt", ios::out | ios::app );
    ob1[i].putdata();
    A_withIndex buffer = {i,ob1[i]};
    W.write( (char *) &buffer, sizeof(buffer));
    W.close();
    cout<<"Congrats! Your object is successfully written to the file \n";
}

Hopefully I kept enough of your code intact to make this legible. Also, I did not run this code through a compiler so it may contain a few errors such as case sensitivity. Method is pretty simple: include an index with your data when reading or writing.

This implementation is by no means efficient since it always appends even when it could have overwritten and reads the whole file to find the last at index. The size_t is also a large overhead. Depending on index range, you may want to reduce to unsigned char and #pragma pack, but if it is that small, you would be better off writing the whole array. I leave making those improvements to you once you understand the concept.

  •  Tags:  
  • c
  • Related