C++ File Handling

C++ has a standard library fstream which is used for file handling. <fstream> header file must be included in your program to process files. It has three data types ofstream, ifstream and fstream.

ofstream

ofstream is the output file stream which is used to create files and to write information to files.

ifstream

ifstream is the input file stream which is used to read information from files.

fstream

fstream is the general file stream which can be used to create files, write information to files, and read information from files.

Opening a file

If you want to write or read from a file first you must open the file. ifstream object is used to open a file to just read from it, ofstream and fstream objects can be used to open a file for writing. The open() function is a member of fstream, ifstream, and ofstream objects. The first argument specifies the name and location of the file and second argument specifies the mode in which the file should be opened.

fstream myFile;
myFile.open(“myFile.txt”, ios::out | ios::in );

Mode Description
ios::app The output to that file will be appended to the end.
ios::ate Move the read/write control to the end of the file.
ios::in Open the file for reading.
ios::out Open the file for writing.
ios::trunc The contents will be truncated before opening the file if it already exists.

Closing a file

The close() function is is a member of fstream, ifstream, and ofstream objects and used to close opened files. C++ automatically flush all the streams, opened files and release allocated memory when a program terminates. But it is good practice to close all the opened files before program termination. Following is the syntax to close a file.

void close();

Writing & reading from a file

The stream insertion operator (<<) is used with the ofstream or fstream object to write information to a file. The stream extraction operator (>>) is used with the ifstream or fstream object to read information from a file.

File Handling Example

#include <fstream>
#include <iostream>

using namespace std;

int main () {

char data[50];

ofstream myFile;
myFile.open(“myFile.txt”);

cin.getline(data, 50);

//writing information into the file
myFile << data << endl;

//closing file
myFile.close();

//opening the file in read mode
ifstream oldFile;
oldFile.open(“myFile.txt”);

oldFile >> data;

//printing the data on screen.
cout << data << endl;

//closing file
oldFile.close();

return 0;

}

C++ Type Casting

Type casting causes the program to treat a variable of one type such as an int to act like another type such as a char for one single operation. The implicit castings are automatically performed when value of a variable is copied to another variable of a compatible type.

Implicit Casting Example

#include <iostream>
using namespace std;
int main()
{

short distance = 24;
int milage;
milage = distance;
cout << “milage = ” << milage;
return 1;

}

In this example the value of distance is copied from ‘short’ to ‘int’ without using type cast operator.

C++ Type Casting

In c++ many conversions require an explicit conversion and a cast operator is used to force one data type to act like another. There is also a functional notation which can be used for type casting. The cast operator is unary and has the same precedence as other unary operators.

#include <iostream>
using namespace std;
int main()
{

double a = 41.63;
double b = 54.23;
int c, d;

c = (int) a;
cout << “Value of c = ” << c << endl ;

d = int(b);
cout << “Value of d = ” << d << endl ;
return 1;

}

Output

Value of c = 41
Value of d = 54

C++ Classes

The main difference between c and c++ programs is that c++ adds object orientation to the c programs. The classes are the central concept of object-oriented programming. Classes are an expanded concept of data structures which can contain data members and member functions and also called user-defined types.

Defining C++ Classes

A class defines a blueprint for a data type which means it does not actually define any data but defines that what the object of the class will consist of and the operations which can be performed on the object.
class keyword is used to define a class in c++.

class className {

accessSpecifier:
member(s);

} objects;

The body of the class can contain members which can be data members, functions or access specifiers.

C++ Access Specifiers

The access specifiers modify the access rights for the members of that class. C++ has following access specifiers.

The private Access Specifier

private members of a class can only be accessed from within members of the same class.

The protected Access Specifier

protected members of a class can only be accessed from within members of the same class and also from members of their derived classes.

The public Access Specifier

public members of a class can be accessed from anywhere where the object is visible.

Defining Class Objects

The declaration of the objects are exactly the same as of the declaration of variables of basic types.

className obj1;
className obj2;

C++ Class Example

#include <iostream>

using namespace std;

class Vehicle {
public:
int model;
double mileage;
string make;
};

int main( ) {
Vehicle car1;
Vehicle car2;

car1.model = 2017;
car1.mileage= 45671;
car1.make = “Honda”;

car2.model = 2015;
car2.mileage= 91175;
car2.make = “Audi”;

cout << “Car 1 is ” << car1.make << ” ” << car1.model << ” with ” << car1.mileage<< ” mileage” <<endl;

cout << “Car 2 is ” << car2.make << ” ” << car2.model << ” with ” << car2.mileage<< ” mileage” <<endl;

return 0;
}

The Output will be

Car 1 is Honda 2017 with 45671 mileage
Car 2 is Audi 2015 with 91175 mileage