GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 191 of 203    Print  
Write Object into a File
How will you write object into file using file concepts in C++?


  
Total Answers and Comments: 2 Last Update: February 12, 2009     Asked by: karthik_knight2000 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: chaamurah
 
#include
#include           //for i/o operations with respect to files


class Employee
{
int id;
        char name[20];
public:
void set_details()
{
cout<<"enter name"<
cin>>name; //dont give blanks in between the name
cout<<"enter id"<
cin>>id;
}
void display_details()
{
cout<<"name : "<
cout<<"id :"<
}
};

int main()
{
//open the file file_name for writing objects.fstream object used for both input and output operations with respect to files
fstream fobj("file_name.txt",ios::out|ios::app);

Employee eobj;
char ch;
//to write in to file

//this do while loop will take the details of employee to an object and writes in to the file until u press 'n' or than 'y'

do
{
cout<<"enter the employee details"<
eobj.set_details();
//using write method we can write in to the file 
fobj.write ( (char *)(&eobj) , sizeof(eobj) );
cout<<"do u want to  enter one more record (Y OR N)"<
cin>>ch;
}while(ch=='y' || ch =='Y')

fobj.close();

//to read from the file

fobj.open("file_name.txt",ios::read);

//read all the objects using read method and displaying.when read encounters end of file returns null.

while(  fobj.read (  (char *)&eobj , sizeof(eobj) )  )
fobj.display_details();
fobj.close();
}


Above answer was rated as good by the following members:
narasimulu
February 03, 2009 00:08:34   #1  
chaamurah Member Since: February 2009   Contribution: 6    

RE: Write Object into a File
#include
#include //for i/o operations with respect to files


class Employee
{
int id;
char name[20];
public:
void set_details()
{
cout<< enter name <
cin>>name; //dont give blanks in between the name
cout<< enter id <
cin>>id;
}
void display_details()
{
cout<< name : <
cout<< id : <
}
};

int main()
{
//open the file file_name for writing objects.fstream object used for both input and output operations with respect to files
fstream fobj( file_name.txt ios::out|ios::app);

Employee eobj;
char ch;
//to write in to file

//this do while loop will take the details of employee to an object and writes in to the file until u press 'n' or than 'y'

do
{
cout<< enter the employee details <
eobj.set_details();
//using write method we can write in to the file
fobj.write ( (char *)(&eobj) sizeof(eobj) );
cout<< do u want to enter one more record (Y OR N) <
cin>>ch;
}while(ch 'y' || ch 'Y')

fobj.close();

//to read from the file

fobj.open( file_name.txt ios::read);

//read all the objects using read method and displaying.when read encounters end of file returns null.

while( fobj.read ( (char *)&eobj sizeof(eobj) ) )
fobj.display_details();
fobj.close();
}

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
February 12, 2009 20:13:28   #2  
sim_sam Member Since: February 2009   Contribution: 3    

RE: Write Object into a File
A function in the class definition can be included which writes to the standard file stream and takes the name of target file as a parameter. Function prototype will be like this:

WriteObjectToFile (const char* fileName)
{
ofstream fout(fileName);
fout<< myVar1<<myVar2<<endl;
fout.close();
}


 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    

 Related Questions

Object is a software bundle of variables and related methods. Objects have state and behavior.    
Latest Answer : An object is an instance of the class through which we can access the methods and class variables ...

 A virtual function allows derived classes to replace the implementation provided by the base class. The compiler makes sure the replacement is always called whenever the object in question is actually 
Latest Answer : It is a functions whose behaviour can be overriden with an inherited class by a function of same signature. It is an important part of OOPS and Polymorphism. ...

"Poly" means "many" and "morph" means "form". Polymorphism is the ability of an object (or reference) to assume (be replaced by) or become many different forms 
Latest Answer : C++ enables polymorphism - The ability of obejcts of different classes related by inheritance respond differntly to the same function call. ...

 Classes and objects are separate but related concepts. Every object belongs to a class and every class contains one or more related objects.  Ø      A Class 
Latest Answer : Class is a combination of data member & member function where object is used for calling function In class we declare In object we call ...

Packaging an object’s variables within its methods is called encapsulation.   
Latest Answer : According to me encapsulation is the process in which we can collect the data members and functions in one place or we can say that to encapsulate the related things. ...

Runtime type identification (RTTI) lets you find the dynamic type of an object when you have only a pointer or a reference to the base type. RTTI is the official way in standard C++ to discover the type 
Latest Answer : We can only assign a derived object to a base object pointer and a base class member to a derived class member pointer. ...

Namespaces allow us to group a set of global classes, objects and/or functions under a name. To say it somehow, they serve to split the global scope in sub-scopes known as namespaces.  The form to 
Latest Answer : Namespace is simply a name given to a global space, to form a sub-space in global space. ...

Virtual destructors: If an object (with a non-virtual destructor) is destroyed explicitly by applying the delete operator to a base-class pointer to the object, the base-class destructor function (matching 
Latest Answer : Virtual constructor is not build-in C++ feature but it doesn't mean its not used by devs in code and in conversations. There are many other things that doesn't exist in particular language yet, people find ways around to solve it (SingleTon, Virtual Constructor, ...

What is the Basic nature of "cin" and "cout" and what concept or principle we are using on those two?
Basically "cin and cout" are INSTANCES of istream and ostream classes respectively.And the concept which is used on cin and cout is operator overloading. Extraction and Insertion operators are 

Latest Answer : if the solution is needed for the unix platform. Then you can create a pthread at the bottom of the program, take the current process id of the parent and make it a deamon by using the standard unix calls like detach(), chdir().. etc. Check if the parent ...
Read Answers (3) | Asked by : suji


 Sponsored Links

 
Related Articles

ODP.NET - OracleCommand Object

ODP NET More About the OracleCommand Object Till now we have seen OracleCommand working with OracleDataReader OracleCommand is not simply meant for OracleDataReader It has got a lot of functionality for itself Let us see few of the most commonly used features of OracleCommand in this section We will
 

Microsoft AJAX Library - Creating Object Members on the Fly

Creating Object Members on the Fly One major difference between OOP in C and ASP NET and OOP in JavaScript is that JavaScript allows creating object members on the fly&quot; This is true for objects and classes that you create yourself and also for JavaScript s own objects and types as well He
 

Microsoft AJAX Library - Object-Oriented JavaScript

Object Oriented JavaScript Objects and classes are implemented differently in JavaScript than in languages such as C VB NET Java or C However when it comes to using them you ll feel on familiar ground You create objects using the new operator and you call their methods or access their fields using t
 

Concepts of Object-Oriented Programming

Object Oriented JavaScript In this chapter you ll learn about OOP Object Oriented Programming and how it relates to JavaScript As an ASP NET developer you probably have some experience working with objects and you may even be familiar with concepts such as inheritance However unless you re already a
 

Object-Oriented Client-Server Internet

Object Oriented Client Server Internet OCSI Environments as IT Infrastructure Client Server Basics Object Oriented Client Server Internet OCSI environments provide the IT infrastructure for supporting OCSI applications For our purposes infrastructure refers to operating systems networks middleware
 

JavaScript String Object

JavaScript String Object In this JavaScript tutorial you will learn about String Object purpose of string object in JavaScript purpose of string object indexof method lastIndexOf method and substring method along with syntax and example mosgoogle center Purpose of String Object in JavaScript The mai
 

JavaScript Date Object

JavaScript Date Object In this JavaScript tutorial you will learn about date object and methods of date object explained along with syntax and example mosgoogle center JavaScript Date Object Usage of Date Object Date object of Java Script is used to work with date and times General syntax for defini
 

What is Data File

In a logical data model, the conceptual data model which is based on the business semantic is being defined. Thus, entities and relationships and corresponding table and column design, object oriented classes, and XML tags, among other things are being laid regardless of the database will be physica
 

SQL Programming

SQL Programming Overview Anybody who has done something for a long time has probably wanted to change how things work at some point or another. A worker at a mill might have found a more efficient way of cutting logs, or a mathematics teacher might have had a hand in changing a school&rsquo;s al
 

The Interview Snafu

How to turn someone else&rsquo;s mistake to your advantage Your dream job is about to become reality. A recruiter gave you the heads up about the perfect position at Humungous Conglomerate, Inc. You went through five interviews as well as a battery of psychological tests mandated by their HR de
 

About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape