C++ Working With Files
Many real life scenarios are there that handle large number of data, and in such situations you need to use some secondary storage to store the data. The data are stored in the secondary device using the concept of files. Files are the collection of related data stored in a particular area on the disk. Programs can be written to perform read and write operations on these files.
Working with files generally requires the following kinds of data communication methodologies:
So far we have learned about iostream standard library which provide cin and cout methods for reading from standard input and writing to standard output respectively. In this chapter you will get to know about how files are handled using C++ program and what are the functions and syntax used to handle files in C++.
Here is the lists of standard file handling classes
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:
Files can be opened in two ways. They are:
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 to it. The syntax of opening a file in C++ is:
open (filename, mode);
There are some mode flags used for file opening. These are:
Closing a file in C++
When any C++ program terminates, it automatically flush 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
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 in files programmers need to use a ofstream or fstream object instead of the cout object and ifstream or fstream object instead of the cin object.
Example:
#include <iostream>
#include <fstream.h>
void main() {
ofstream file;
file.open("egone.txt");
file & lt; & lt;
"Writing to a file in C++....";
file.close();
}
Another Program for File Handling in C++
Example:
#include <iostream>
#include<fstream.h>
void main() {
char c, fn[10];
cout & lt; & lt;
"Enter the file name....:";
cin & gt; & gt;
fn;
ifstream in (fn);
if (! in ) {
cout & lt; & lt;
"Error! File Does not Exist";
getch();
return;
}
cout & lt; & lt;
endl & lt; & lt;
endl;
while ( in .eof() == 0) { in .get(c);
cout & lt; & lt;
c;
}
}
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 & lt; & lt;
"Hello GS";
fileo.close();
ifstream fin("Filethree");
char ch;
while (fin) {
fin.get(ch);
cout & lt; & lt;
ch;
}
fin.close();
}
No comments:
Post a Comment