read from/write to file

Could anybody give me a simple exaple about how to store information in a file and retrieve then again
kogerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sureshkumarCommented:
# include<iostream.h>
# include<fstream.h>

class test
{
private:
int n;
char name[20];
public:
void getdata();
void putdata();
};

void test::getdata()
{
 cout<<"ente the number";
 cin>>n;
 cout<<"enter the name";
 cin>>name;
}

void test::putdata()
{
  cout<<"number is :"<<n<<"\t"<<"name is :"<<"\t"<<name;
}

int main()
{
  fstream finout("data.dat"); //opening file in input output mode
 
  test o1;  // creating object o1 for class test

  o1.getdata();
  finout.write((char*)&o1, sizeof(o1));  //write on to file
  finout.read((char*)&o1, sizeof(o1));  //read from file
  o1.putdata();

  finout.close();
  return 0;
}

hope this will help you.
further queries come back

suresh kumar
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kogerAuthor Commented:
Hi

in the write line, what does this mean?
(char*)&o1

Is this the normally used way to write to file today, it seems a little DOS'ed


Can you explain me how this works

reinterpret_cast<char * >( &x[1] )

x: array of char
0
kogerAuthor Commented:
Hi

in the write line, what does this mean?
(char*)&o1

Is this the normally used way to write to file today, it seems a little DOS'ed


Can you explain me how this works

reinterpret_cast<char * >( &x[1] )

x: array of char
0
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

sureshkumarCommented:
(char*)&o1

what ever in the object on o1 (i.e., either int or char or double) it will be stored as strings in file.

thanks
suresh kumar

0
kogerAuthor Commented:
ok you didn't what to comment:

Is this the normally used way to write to file today, it seems a little  DOS'ed?

Thanks
koger
0
sureshkumarCommented:
hi koger

Yeah, till today i used this.If you know updated way( i.e., writing in to file). please tell me,I am waiting for your answer.Since i am very intersted to know new things.otherwise give me sometime i will confirm and comeback to you

thanks
suresh kumar
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.