C++ Tutorial
Class and Object
Reference
Inheritance and Derivation
Polymorphism and Virtual Functions
Operator Overloading
Template
Exception
Object Oriented Advanced
Input/Output Stream
File Operations
In C++, file handling is achieved using file streams, which are classes provided by the C++ Standard Library. The two main classes for handling files are ifstream
(input file stream) for reading and ofstream
(output file stream) for writing. Both classes are part of the fstream
library. Alternatively, the fstream
class can be used for both reading and writing.
In this tutorial, we'll cover the basics of file handling using the ifstream
, ofstream
, and fstream
classes in C++.
fstream
header.#include <iostream> #include <fstream>
ofstream
class and use the open()
function to open a file for writing. The <<
operator is used to write data to the file. Finally, use the close()
function to close the file.Example:
#include <iostream> #include <fstream> int main() { std::ofstream outfile("output.txt"); if (!outfile.is_open()) { std::cerr << "Error opening file for writing." << std::endl; return 1; } outfile << "Hello, World!" << std::endl; outfile << "This is a C++ file handling tutorial." << std::endl; outfile.close(); return 0; }
ifstream
class and use the open()
function to open a file for reading. The >>
operator can be used to read data from the file, or the getline()
function to read an entire line. Finally, use the close()
function to close the file.Example:
#include <iostream> #include <fstream> #include <string> int main() { std::ifstream infile("output.txt"); if (!infile.is_open()) { std::cerr << "Error opening file for reading." << std::endl; return 1; } std::string line; while (std::getline(infile, line)) { std::cout << line << std::endl; } infile.close(); return 0; }
fstream
for Reading and Writing:
The fstream
class can be used for both reading and writing. You'll need to specify the file mode while opening the file, using flags like std::ios::in
(for reading), std::ios::out
(for writing), and std::ios::app
(for appending).Example:
#include <iostream> #include <fstream> #include <string> int main() { std::fstream file("example.txt", std::ios::in | std::ios::out | std::ios::app); if (!file.is_open()) { std::cerr << "Error opening file." << std::endl; return 1; } // Write to the file file << "This is an example using fstream." << std::endl; // Move to the beginning of the file file.seekg(0, std::ios::beg); // Read and display the content of the file std::string line; while (std::getline(file, line)) { std::cout << line << std::endl; } file.close(); return 0; }
These examples demonstrate the basics of file handling in C++ using ifstream
, ofstream
, and fstream
.
How to use the file class in C++:
Using the file class in C++ involves creating instances of the ifstream
or ofstream
classes for input and output operations, respectively. Example:
#include <iostream> #include <fstream> int main() { std::ofstream outputFile("example.txt"); // ofstream for output // or std::ifstream inputFile("example.txt"); // ifstream for input // Your file operations go here outputFile.close(); // Close the output file if opened inputFile.close(); // Close the input file if opened return 0; }
File input/output in C++ using file stream class:
File input/output in C++ is accomplished using file stream classes (ifstream
for input and ofstream
for output). Example:
#include <iostream> #include <fstream> int main() { std::ofstream outputFile("output.txt"); // Output file stream std::ifstream inputFile("input.txt"); // Input file stream // Your file operations go here outputFile.close(); // Close the output file if opened inputFile.close(); // Close the input file if opened return 0; }
Opening and closing files in C++ with file class:
Opening and closing files in C++ involves using the open
method to open a file and the close
method to close it. Example:
#include <iostream> #include <fstream> int main() { std::ofstream outputFile; outputFile.open("example.txt"); // Opening the file // Your file operations go here outputFile.close(); // Closing the file return 0; }
Reading and writing to files in C++ using file stream:
Reading and writing to files in C++ involves using the >>
and <<
operators for formatted input and output. Example:
#include <iostream> #include <fstream> #include <string> int main() { std::ofstream outputFile("output.txt"); // Writing to the file outputFile << "Hello, File Stream!\n"; std::ifstream inputFile("output.txt"); // Reading from the file std::string line; getline(inputFile, line); std::cout << "Read from file: " << line << std::endl; outputFile.close(); inputFile.close(); return 0; }
File stream flags in C++:
File stream flags in C++ provide control over various aspects of file I/O. Example:
#include <iostream> #include <fstream> int main() { std::ofstream outputFile("example.txt"); // Set flags for formatting outputFile.setf(std::ios::left); // Left-align output // Your file operations go here outputFile.close(); return 0; }
Binary file handling in C++ using file class:
Binary file handling in C++ involves using the ios::binary
flag for binary input/output. Example:
#include <iostream> #include <fstream> int main() { std::ofstream outputFile("binaryfile.bin", std::ios::binary); // Your binary file operations go here outputFile.close(); return 0; }
File position pointers in C++ file stream:
File position pointers in C++ (tellg
and seekg
) are used to get and set the position of the file stream. Example:
#include <iostream> #include <fstream> int main() { std::ifstream inputFile("example.txt"); // Get current position std::streampos position = inputFile.tellg(); // Your file operations go here // Set position back to the saved position inputFile.seekg(position); inputFile.close(); return 0; }
File stream modes in C++:
File stream modes in C++ define how a file should be opened, such as std::ios::in
for input and std::ios::out
for output. Example:
#include <iostream> #include <fstream> int main() { std::ofstream outputFile("example.txt", std::ios::out | std::ios::app); // Your file operations go here outputFile.close(); return 0; }
Sequential and random access file operations in C++:
Sequential access involves reading or writing data sequentially, while random access uses file position pointers for non-sequential access. Example:
#include <iostream> #include <fstream> int main() { std::fstream file("example.txt", std::ios::in | std::ios::out); // Sequential access file << "Sequential Access\n"; // Random access file.seekp(0); // Move to the beginning file << "Random Access\n"; file.close(); return 0; }
Error handling with file stream in C++:
Error handling with file streams in C++ involves checking the stream's state using fail
or bad
functions. Example:
#include <iostream> #include <fstream> int main() { std::ifstream inputFile("nonexistent.txt"); // Check for errors if (inputFile.fail()) { std::cerr << "Error opening file." << std::endl; return 1; } // Your file operations go here inputFile.close(); return 0; }
File stream manipulators in C++:
File stream manipulators in C++ modify the behavior of the stream, such as std::setw
for setting the width of the next input or output field. Example:
#include <iostream> #include <iomanip> int main() { std::ofstream outputFile("example.txt"); // Set the width of the next output field outputFile << std::setw(10) << "Hello"; outputFile.close(); return 0; }
C++ file class vs C-style file operations:
The C++ file class provides a more object-oriented and convenient interface compared to C-style file operations. Example:
#include <iostream> #include <fstream> int main() { // C++ file class std::ofstream outputFile("example.txt"); outputFile << "C++ File Class\n"; outputFile.close(); // C-style file operations FILE* cFile = fopen("example.txt", "a"); fprintf(cFile, "C-style File Operations\n"); fclose(cFile); return 0; }