c++ open using constructor

c++ open using constructor

we learn previous pages about constructor so we know that a constructor is used to initialize an object while it is being created. here the filename is used to initialize the file stream object.

This involves the following steps

1. create first a file stream object to manage the stream using the appropriate class. It is to say the class ofstream is used to create the output stream and the class ifstream to create the input stream
2. Initialize the file object with the desired filename foe example the following statement open a file named “result” for output ofstream outfile(“result”); // output only

it is creates outfile as an ofstream object that manages the output stream. This object can be any valid in C++ programming name such as o_file,myfile or fout. This statement also opens the file result and attaches it to the output stream outfile.

This is in the below fig

two file streams working picture

Similarly, in the below the following statement declares infile as an ifstream object and attaches it to the file data for reading(input)
ifstream infile(“data”); // input only
The program may contain statement like

The first method is useful only when we use only one file in the stream. The second method is useful when we want to manage multiple files using one stream

outfile<<“Total”;
outfile< infile>>number;
infile>>string;

two file stream working on one file picture

we can use the same file for both reading and writing data as show . The program would contain the following statements

program1
…….
…….
ofstream outfile(“salary”); // creates outfile and connects
//”salary” to it
…..
…..
program2
…….
…….
ifstream infile(“salary”); // creates infile and connects
//”salary” to it
……
……

The connection with a file is closed automatically when the stream object expires(when the program terminates) In the above statement. when the program one is terminated, then the salary file is disconnected from the outfile stream same as action takes place when the program2 terminates Instead of using two programs one for writing data (output) and another for reading data (input) we can use a single program to do both the operations on a file

Example
…….
……
outfile.close(); // disconnect salary from outfile
ifstream infile(“salary”); // and connect to infile
…….
……
infile.close(); // disconnect salary from infile
although we have used a single program. we created two file stream objects, outfile(to put data to the file) and infile (to get data from the file).
Note that the use of a statement like
outfile.close();

disconnect the file salary from the output stream outfile. Remember the object outfile still exists and the salary file may again be connected to outfile later or to any other stream. In this example, we are connecting the salary file to infile stream to read data

in the below program uses a single file for both writing and reading the data . First it takes data from the keyboard and writes to the file. after the writing is completed. the file is closed. The program again open the same file, reads the information already written to it and display the same on the screen

WORKING WITH SINGLE FILE

// creating files with constructor function
#include <iostream>
#include <fstream>
int main()
{
std::ofstream outf("ITEM"); //connect ITEM file to outf
std::cout << "Enter item name";
char name[30];
std::cin >> name; // get name from key board and
std::cout << name << "\n"; // write to file ITEM
std::cout << "Enter item cost:";
float cost;
std::cin >> cost; // get cost from key board and
outf << cost << "\n"; // write to file ITEM
outf.close(); // disconnect ITEM file from outf
std::ifstream inf("ITEM"); // connect ITEM File to inf
inf >> name; // read the name from file ITEM
inf >> cost; // read the cost from file ITEM
std::cout << "\n";
std::cout << "Item name:" << name << "\n";
std::cout << "item cost:" << cost << "\n";
inf.close(); // disconnect ITEM from inf
}

out of the above program
Enter ITem name:CD-ROM
Enter item cost:400
Item name:CD-ROM
item cost:250

  • 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