Question

I have created a program which writes an object to a file(by converting it to char) and then retrieve the records from the file and assign it to variables. .

Here is my Problem in detail:

I have a person class with two member variables called name and Address.
I create a person class object through my program and then write it to the file.
When i retrieve back the record i am only able to retrieve the name but i cannot retrieve the address.

Here is my code:-

My main class

int _tmain(int argc, _TCHAR* argv[])
{
    Person p1;
    p1.setName("Jim");
        p1.setAddress("USA");
    Person p2;


    File writeToFile;

       writeToFile.writeObject(p1); //write the object to the file using method


    fstream readFile("Filet.txt",ios::in); //creates an object of fstream to retrieve records

    // declared 2 variable to hold the values
    string name;               
    string address;

    //loop until its end of the file
    while(!readFile.eof())
    {
    readFile>>name>>address; //assign the record to the variables
    }

    p2.setName(name);  //initialize the object's members
    p2.setAddress(address);


    cout<<p2.getName()<<""<< p2.getAddress()<<endl; //print the values
    system("pause");

return 0;
}

Here is what the program outputs:

enter image description here

What seems to be the problem here ?

Thank you for your time

Edit:-

My Person Class Implementation

Person::Person(){}
void Person:: setName(string tname)
        {
            int length=tname.size();
            length=length<5 ? length : 4;
            tname.copy(name,length);
            name[length]='\0';
        }



 string Person::getName()
        {
           return this->name;
        }
void Person:: setAddress(string taddress)
 {
     int length=taddress.size();
     length=length<15 ? length:14;
     taddress.copy(address,length);
     address[length]='\0';
 }

 string Person:: getAddress()
{
    return this->address;
}

My writeObject method implementation

string File:: writeObject (Person obj)
        {


            fstream writeFile;
            writeFile.open("Filet.txt",ios::out |ios::in);

            if(!writeFile)
            {
                cout<<"No File"<<endl;
            }

           else
            {


                writeFile.write(reinterpret_cast<const char *>  
                                (&obj),sizeof(Person));

                outFile.close();
            }
            return "Done";

         }
Was it helpful?

Solution

The "garbage written" in the output file might be due to this line:

writeFile.write(reinterpret_cast<const char *>(&obj),sizeof(Person));

reinterpret_cast will not add spaces for you automatically which is how logically the operator>> file reading would be working based on words separated by spaces.

I would say, reinterpret_cast should always be the last resort if there are better options. Based on your existing write functionality, I will assume that you prefer to write the file in text mode instead of binary.

I would simple use the following mechanism:

string out_str = obj.getName() + " " + obj.getAddress() + " ";
writeFile.write(out_str.c_str(), out_str.size());

I would personally write them line-by-line as opposed to word-by-word. Also, you will have issues if the names and addresses appear to be more than just one word, so be aware of that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top