C++ open files using open()

C++ open files using open()

As started earlier, the function open() can be used to open multiple files that use the same stream object. for example we may want to process a set of files sequentially. in such cases, we may create a single stream object and use it to open each in turn.

This is done as follows

file-stream-class stream-object;
stream-object.open(“filename”);

Example
ofstream outfile; // create stream(for output)
outfile.open(“DATA1”); // connect stream to DATA1
……..
……..
outfile.close(); // disconnect stream from DATA1
outfile.open(“DATA2”); // connect stream to DATA2
…….
…….
outfile.close(); // disconnect stream from DATA2
….
…..

The above example of program segment opens two files in sequence for writing the data. Note that the first file should be closed before opening the second one. This is necessary because a stream can be connected to only one file at a time see in the below program

WORKING WITH MULTIPLES FILES
// creating files with open() function
#include <iostream>
#include <fstream>
int main()
{
std::ofstream fout; //create the output stream
fout.open("country"); // connect "country" to it
fout<<"United states \n";
fout<<"united kingdom \n";
fout<<"south korea \n";
fout.close(); // disconnect the country
fout.open("capital"); // and connect the capital
fout<<"washington \n";

fout<<"London \n";
fout<<"seoul \n";
fout.close(); // disconnect the "capital"
// reading the files
const int n=80; // size of line
char line[n];
std::ifstream fin; // create input stream
fin.open("country"); // connect country to it
std::cout << "the contents of country file \n";
while (fin) // check end of file
fin.getline(line,n); // ready a line
std::cout << line; // display it

fin.close(); // disconnect country
fin.open("capital"); // and connect to capital
std::cout << "\n contents of capital file \n";
while(fin)
{
fin.getline(line,n);
std::cout << line;
}
fin.close();
}

output:
Contents of country file
United states
united kingdom
south korea
contents of capital file
washington
london
seoul

two file stream working on one file picture

At times we may require to use two or more files simultaneously. for example we may require to merge to sorted files into a third sorted file. This means both the sorted files have to be kept open for reading and the third on kept open for writing. In such cases, we need to create two separate input streams for handling the two input files and one output stream for handling the output file.

READING FROM TWO FILES SIMULTANEOUSLY
// Reads the files created in program
#include < iostream>
#include <fstream>
#include <stdlib.h>
int main()
{
const int SIZE=80;
char line[SIZE];
std::ifstream fin1,fin2; // create two input streams
fin1.open("country");
fin2.open("capital");
for (int i=1;i<=10;i++)
{
if (fin1.eof()!=0)
{
std::cout << "Exit from country \n";
exit (1);
}
fin1.getline(line,SIZE);
std::cout << "capital of " << line;
if(fin2.eof()!=0)
{
std::cout << "Exit from capital \n";
exit (1);
}
fin2.getline(line,SIZE);
std::cout << line << "\n";
}
}

output:
capital of united states of America
Washington


capital of united kingdom
London
capital of south korea
seoul

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise