Monday, 19 March 2018

Object Oriented Programming: No. 12


No: 12


1. Experiment vision: Write a C++ program that creates an output file, writes information to it; closes the file and open it again as an input file and read the information from the file.
2. OBJECTIVE: Program to implement File related program, to create a file and writing operation will be done after that close the file. Again open the same file in reading mode.
3. THEORY: So far, we have been using the iostream standard library, which provides cin and cout methods for reading from standard input and writing to standard output respectively. Now we will see how to read and write from a file. This requires another standard C++ library called fstream.
ofstream: This data type represents the output file stream and is used to create files and to write information to files.
ifstream: This data type represents the input file stream and is used to read information from files.
fstream: This data type represents the file stream generally, and has the capabilities of both ofstream and ifstream which means it can create files, write information to files, and read information from files.
Files/streams are the set of characters, using C++ programs, we will learn to write, read data in, from the files (both text and binary).
4. PROGRAM:
#include<fstream>
#include<iostream>
using namespace std;
class student
{ public:
char name[20];
int roll_no;
void getdata()
{ cout<<"enter name";
cin>>name;
cout<<"enter roll no";
cin>>roll_no;
}
void putdata()
{ cout<<"name:"<<name;
cout<<"roll is"<<roll_no;
}
};
int main()
{
student stud, stud1;
stud.getdata();
ofstream of("data.txt");
of.write((char *) &stud, sizeof(stud)); //writing object…
of.close();
ifstream f("data.txt");
f.read((char *) &stud1, sizeof(stud1)); //reading object..
stud1.putdata();
return 0;
}
5. OUTPUT:










  1. CONCLUSION: Thus using C++ program we have successfully executed the program for file related operations.
7. PRECAUTIONS:
1. Avoid wrong keywords.

2. Check whether all brackets are closed properly or not.

3. Take desired output.

4. In case of abnormal results, Contact the teacher/instructor, repeat the experiment, and check the algorithm and program.


7. REMARK:
A file related basic operations such as create a new file, writing data into a file, close a file. Again open a file in reading mode and close the file.
8. Discussion Questions:
1) Explain File in detail.
____________________________________________________________________________________________________________________________________________________________
2) What are the types of file opening?
____________________________________________________________________________________________________________________________________________________________
3) What is EOF?
____________________________________________________________________________________________________________________________________________________________


No comments:

Post a Comment

About Me

Hi, I am Prof. Amol Zade, working in the field of teaching and research from last more than 10 years. My area of interests are Artificial Intelligence, Wireless Networking, Algorithms. I have 16 International paper publications along with 4 International Conference; also 3 Books published. published on Object Oriented Programming with open source approach.