C++ Working With Files - Devbhoomi

FREE JOB ALERT & ONLINE TUTORIALS

Hot

Post Top Ad

Sunday 8 October 2017

C++ Working With Files

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:
  • Data transfer between console units
  • Data transfer between the program and the disk file
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

  1. Ofstream: This file handling class in C++ signifies the output file stream and is applied to create files for writing information to files
  2. Ifstream: This file handling class in C++ signifies the input file stream and is applied for reading information from files
  3. 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:
  1. Using constructor function of the class
  2. 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 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:
  • 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 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

  1. open(): To create a file
  2. close(): To close an existing file
  3. get(): to read a single character from a file
  4. put(): to write a single character in file
  5. read():to read data from file
  6. write(): to write data into 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 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

Post Top Ad