File Operations Using C++ File Streams fstream, ifstream, ofstream

File Operations Using C++ File Streams fstream, ifstream, ofstream

In the C++ programming language, file handling data read and write operations are handled using the fstream class library. It provides classes for reading files from existing secondary storage locations and for creating and writing data and information to those files.

File Operations Using C++ File Streams fstream, ifstream, ofstream

The most commonly used file handling stream classes are.

  • ifstream – This is an input file stream class library for file handling operations. It is used to read data from existing files.
  • ofstream – This is an output file stream class library for file handling operations. It is used to create or write new files.
  • fstream – This is a file stream class library for file handling tasks. It is used to read data from existing files and write data and information to files.

The classes listed above help C++ programmers perform file operations. File handling involves opening files, reading data and information from files, creating or writing data, checking for activity in existing files, and finally closing files.

Below is an overview of how to use these classes for file handling operations in C++.

Using the <fstream> class library.

To use the File Stream class library in your program for file handling, C++ users can add the <fstream> header file or pre-processor to the top.

#include <iostream>

#include <fstream> // Use this for file handling tasks for file handling tasks.

#include <string> // Use this for file handling tasks.

using namespace std;

Writing to a file (using the ofstream class method).

To create or write data to a text or binary file in a file handling process, C++ users can use the ofstream (output file stream) class. If the file doesn’t exist on your system, it will first create it. If the file exists, it overwrites its contents and information. Unless C++ users use the append file handling mode.

Example of basic file handling (writing to a file).

#include <iostream>

#include <fstream>

using namespace std;

int main() {

// Here we create an ofstream object and open the file in write mode only

ofstream outFile(“samplefile.txt”);

// Here it checks if the file is open for writing purposes

if (outFile.is_open()) {

outFile << “Welcome to Vcanhelpsu” << endl; // Here we start writing data and text to the file

outFile << “Let’s create content in samplefile text.” << endl;

outFile << “Let’s learn file handling in detail” << endl;

outFile.close(); // Here we close the file after writing the operation

cout << “Samplefile Data written successfully.” << endl;

} else {

cout << “Restrict to open file.” << endl;

}

return 0;

}

Explanation of basic file handling (writing to a file).

  • In this example, ofstream outFile(“samplefile.txt”) creates a file named samplefile in the current storage location and opens the file in write mode.
  • outFile << “data” creates data in the existing file.
  • outFile.close() closes the file after writing new data and information to the existing file.

Remember, if the file exists on your system, it truncates it (i.e., empties it) before writing new data to it. To avoid this process, use the ios::app flag (append mode) when opening a file in file handling.

Reading from a file in file handling (using the ifstream class method).

To read data from an existing file in file handling, use the ifstream (input file stream) class. The file is opened in read mode by default.

Example of basic file handling (reading from a file).

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

int main() {

// Here we create an ifstream object and open the file in read mode only

ifstream inFile(“samplefile.txt”);

// Here it checks if the file is open or not

if (inFile.is_open()) {

string line;

// here it reads each line from the file

while (getline(inFile, line)) {

cout << line << endl; // here it prints the line to the console screen

}

inFile.close(); // here it close the file after reading purpose only

} else {

cout << “Restrict to open file.” <<endl;

}

return 0;

}

Explanation of basic file handling (reading from a file).

  • In this example, ifstream inFile(“samplefile.txt”) opens the current file in read mode.
  • Getline(inFile, line) reads one line at a time from the file.
  • The file text line loop continues until all file lines have been read.

Reading and writing to the same file in file handling (using fstream class methods).

When C++ users need to perform both read and write file handling operations in the same file, C++ programmers can use the fstream class library methods.

Example of file handling (reading and writing with fstream).

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

int main() {

//here it open the file for both reading and writing purpose only

fstream file(“samplefile.txt”, ios::in | ios::out);

if (!file) {

cout << “Display Error working with file.” <<endl;

return 1;

}

// here it reads data from the existing file

string line;

cout << “Start Reading from file.” <<endl;

while (getline(file, line)) {

cout << line << endl; // here it prints the line to the console screen

}

// here it writes new data to the active file

file.clear(); //here it clears EOF flag

file.seekp(0, ios::end); //here it move to the end of the file for appending mode purpose

file << “Let append or add new data to the file.” <<endl;

file.close(); // Here it closes the file after the operation

cout << “File data written to successfully.” << endl;

return 0;

}

Explanation of file handling (reading and writing with fstream).

  • In this example, fstream file(“samplefile.txt”, ios::in | ios::out) opens the file for both input and output file mode read and write operations.
  • In file handling, getline(file, line) reads text lines from the file.
  • In file handling, file.clear() clears the EOF flag, allowing us to continue writing to the existing file.
  • In file handling, file.seekp(0, ios::end) moves the write file pointer to the end of the file to append new data to the existing file.

File modes for fstream, ifstream, and ofstream in file handling.

C++ users can indicate different file modes when opening an existing file in file handling. These file handling modes help control how the current file is opened and how the data in the file is accessed and managed.

Popular file handling modes are.

  • ios::in – Opens the file for reading in file handling mode.
  • ios::out – Opens the file for writing in file handling mode. (If the file doesn’t exist, it first creates it; if the file exists, it truncates the file, meaning it empties it.)
  • ios::app – Opens a file in append mode. This means new data is created or written at the end of the file.
  • ios::ate – Opens a file in binary mode. This means it’s used for working with non-text files.
  • ios::binary – Opens a file in binary mode. This means it’s used for working with non-text files.
  • ios::trunc – Truncates a file to zero length if it already exists. This is the default method for fstream.

Example of file handling (opening a file in different modes).

#include <iostream>

#include <fstream>

using namespace std;

int main() {

// here it opens a file for writing and truncate purposes. Default file mode

ofstream outFile1(“samplefile.txt”);

// Here it opens a file for appending only data written at the end of the existing file

ofstream outFile2(“datafile.txt”, ios::app);

// Here it opens a file for reading and writing purposes only without truncating method

fstream outFile3(“information.txt”, ios::in | ios::out | ios::app);

// Here it opens a file in binary mode machine language mode

ofstream outFile4(“binarydata.bin”, ios::binary);

return 0;

}

Explanation of file handling (opening a file in different modes).

  • This file handling mode ofstream outFile1(“samplefile.txt”) opens the file for writing text information. It truncates the file contents, i.e., empties it.
  • This file handling mode ofstream outFile2(“datafile.txt”, ios::app) Opens the file for appending data and information at the end of the file.
  • This file handling mode fstream outFile3(“information.txt”, ios::in | ios::out | ios::app) Opens the file in append mode for both read and write access.
  • This file handling mode ofstream outFile4(“binarydata.bin”, ios::binary) Opens the binary file in binary mode for write access.

Leave a Reply