0

I am using ofstream to write an object of contact manager into the text file using dev C++. My objective is to save the object into the text file so that the Name and Phone can be read from the text file too. Following is my simple code:

#include <iostream>
#include <fstream>
#include<string>
using namespace std;
class phone
{
    int phone;
    string name;

    public:void get()
        {
            cin>>phone;
            cin>>name;
        }
    public:void show()
        {
            cout<<phone<<"-"<<name;
        }

};

int main () {
    phone p;
    p.get();
    p.show();
    ofstream outfile("12.txt"); // Open the file in output mode

    outfile.write((char*)&p, sizeof(p)); // Write the object into the file
    return 0;
}

But when I open the Text file, it shows some chinese Characters. Any help on how to fix it?

Barmar
  • 741,623
  • 53
  • 500
  • 612
Sumit Dhiman
  • 109
  • 3
  • 8
  • Add a method `string get_name() const {...}` and use `outfile << p.get_name();`? By the way, it looks like you're coming from Java or C#. Have you decided which [C++ book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) you want to read? – Zeta Jul 31 '15 at 11:31
  • i read a local author – Sumit Dhiman Aug 01 '15 at 12:31

3 Answers3

2

Writing an object writes the binary representation of the object, it doesn't convert the members to text. Use:

outfile << p.phone << "-" << p.name << endl;

But you'll need to declare these members public for this to work. Or you could define public get_phone and get_name functions that access them, and use them here.

You could also overload the operator<< for your class, see here for an example of how to do this. Then you would be able to write:

outfile << p;
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

You are mixing binary and text formats. You write a binary file, then you try to read it as if it were text, and your text reader interprets the binary data as weird characters. I suggest you stick to text, and modify your show() to allow it to write to a file:

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

class phone
{
  int phone;
  string name;

public:
  void get()
  {
    cin>>phone;
    cin>>name;
  }

  void show(ostream &ostr)
  {
    ostr << phone << "-" << name;
  }

};

int main ()
{
  phone p;
  p.get();
  p.show(cout);
  ofstream outfile("12.txt"); // Open the file in output mode
  p.show(outfile);   
  return 0;
}

Further refinements are possible, once you have this working.

Beta
  • 96,650
  • 16
  • 149
  • 150
0

I rearrange your code, but i have changed most of it. Hope my solution would help.

struct  phone

{

int phone;

string name;

}; int main () {

ofstream outfile("12.txt", ios::out); // Open the file in output mode
          phone p[1];
          cout<<"enter phone first"<<endl;
          cin>>p[0].phone;
          cin>>p[0].name;

outfile<<p[0].phone<<" - "<<p[0].name<<endl; // Write the object into the file
cout<<p[0].phone<<" - "<<p[0].name<<endl;
outfile.close();

        cin.get();
        return 0;

}