Opening and Closing A File in C++

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Ofstream: This file handling class in C++ signifies the output file stream and is applied to create files

for
writing information to files
Ifstream: This file handling class in C++ signifies the input file stream and is applied for reading
information from files
Fstream: This file handling class in C++ signifies the file stream generally, and has the capabilities for
representing both ofstream and ifstream
All the above three classes are derived from fstreambase and from the corresponding iostream class and
they are designed specifically to manage disk files.
Opening and Closing a File in C++
If programmers want to use a disk file for storing data, they need to decide about the following things
about the file and its intended use. These points that are to be noted are:
A name for the file
Data type and structure of the file
Purpose (reading, writing data)
Opening method
Closing the file (after use)
Files can be opened in two ways. They are:
Using constructor function of the class
Using member function open of the class
Opening a File
The first operation generally performed on an object of one of these classes to use a file is the
procedure known as to opening a file. An open file is represented within a program by a stream and any
input or output task performed on this stream will be applied to the physical file associated with it. The
syntax of opening a file in C++ is:
open (filename, mode);
There are some mode flags used for file opening. These are:
ios::app: append mode
ios::ate: open a file in this mode for output and read/write controlling to the end of the file
ios::in: open file in this mode for reading
ios::out: open file in this mode for writing
ios::trunk: when any file already exists, its contents will be truncated before file opening
Closing a file in C++
When any C++ program terminates, it automatically flushes out all the streams releases all the allocated
memory and closes all the opened files. But it is good to use the close() function to close the file related
streams and it is a member of ifsream, ofstream and fstream objects.
The structure of using this function is:
void close();
General functions used for File handling
open(): To create a file
close(): To close an existing file
get(): to read a single character from the file
put(): to write a single character in the file
read(): to read data from a file
write(): to write data into a file
Reading from and writing to a File
While doing C++ program, programmers write information to a file from the program using the stream
insertion operator (<<) and reads information using the stream extraction operator (>>). The only
difference is that for files programmers need to use an ofstream or fstream object instead of the cout
object and ifstream or fstream object instead of the cin object.
Example: #include<iostream>
#include <iostream> #include<fstream>
#include <fstream.h>
int main()
void main () { {
ofstream file; ofstream ofile;
file.open ("egone.txt"); ofile.open ("text.txt");
file << "Writing to a file in ofile << "Computer Science << endl;
C++...."; cout << "Data written to file" << endl;
file.close(); ofile.close();
getch(); return 0;
} }

Another Program for File Handling in C++


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

int main() void main()


{ {
char data[100]; char c,fn[10];
ifstream ifile; cout<<"Enter the file name....:";
cin>>fn;
ifstream in(fn);
//create a text file before
if(!in)
executing.
{
ifile.open ("text.txt");
cout<<"Error! File Does not
while ( !ifile.eof() )
Exist";
{
getch();
ifile.getline (data, 100);
return;
cout << data << endl;
}
}
cout<<endl<<endl;
ifile.close();
while(in.eof()==0)
return 0;
{
}
in.get(c);
cout<<c;
}
getch();
}

Another C++ Program to Print Hello GS to the Console


Example:
#include <iostream>
#include<fstream.h>
#include<math.h>

void main()
{
ofstream fileo("Filethree");
fileo<<"Hello GS";
fileo.close();
ifstream fin("Filethree");
char ch;
while(fin)
{
fin.get(ch);
cout<<ch;
}
fin.close();
getch();
}

#include <iostream>
int main( )
{
cerr << "An error occured";

return 0;
}

#include <iostream>
int main( )
{
char sample[] = " Computer Science ";
cout << sample << " – Study Data File Handling ";

return 0;
}

#include<iostream>
int main()
{
int age;

cout << "Enter your age:";


cin >> age;
cout << "\nYour age is: "<<age;

return 0;
}
/* C++ File Handling - Store information into the file in C++ */

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
ofstream fout;
char fname[20];
char rec[80];
clrscr();

cout<<"Enter file name: ";


cin>>fname;

fout.open(fname, ios::out);
cout<<"Enter a word: ";
cin>>rec;
fout<<rec;
cout<<"Data inserted successfully..!!";

fout.close();
getch();
}
* C++ File Handling - Retrieve information from the file in C++ */

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
ifstream fin;
char fname[20];
char rec[80];
clrscr();

cout<<"Enter file name: ";


cin>>fname;

cout<<"The file contains:\n";


fin.open(fname, ios::in);
fin.get(rec, 80);
cout<<rec;

fin.close();
getch();
}

You might also like