DOSYA İŞLEMLERİ
FILE HANDLING in C++ :
File handling in C++ refers to the process of accessing files, reading data from them, writing data to them, creating, deleting, or modifying files. File operations are performed using a set of functions and classes provided by the standard library in C++, specifically the <fstream>
header file.
File handling typically involves the following steps:
1. Opening a File:
To perform operations on a file, an object of type std::fstream
is created. This object can be used for reading, writing, or both, depending on the opening mode (std::ios::in
, std::ios::out
, std::ios::app
, etc.) specified while opening the file.
#include <fstream>
std::fstream file;
file.open("file.txt", std::ios::out); // Open file in write mode
2. Writing Data to a File:
To write data to an open file, the <<
operator or the write()
function is used.
file << "Hello, writing to a file!\n";
3. Reading Data from a File:
To read data from an open file, the >>
operator or the read()
function is used.
std::string line;
std::getline(file, line); // Read a line from the file
std::cout << "Read line: " << line << std::endl;
4. Closing a File:
It is important to close a file once it is no longer needed. This releases the associated memory resources.
file.close(); // Close the file
During file handling, it is important to handle errors and handle them appropriately if they occur. File opening, writing, or reading operations can fail, leading to unexpected termination of the program. Therefore, proper error checking and handling should be performed.
if (!file) {
std::cerr << "File opening error!" << std::endl;
// Perform necessary actions to handle the error
}
The <fstream>
header file in C++ provides various other functions and details related to file handling. Operations like opening files in binary or text mode, positioning within a file, deleting or renaming files can also be performed. For more detailed functionality and options, refer to the documentation and references for the C++ standard library.
Examples :
1. Writing Data to a File:
#include <fstream>
int main() {
std::ofstream file("data.txt"); // Open file for writing
if (file.is_open()) {
file << "Hello, File Handling!" << std::endl;
file << "This is another line." << std::endl;
file.close(); // Close the file
std::cout << "Data written to file successfully." << std::endl;
} else {
std::cerr << "Failed to open the file." << std::endl;
}
return 0;
}
1. Reading Data from a File :
#include <fstream>
int main() {
std::ifstream file("data.txt"); // Open file for reading
if (file.is_open()) {
std::string line;
while (std::getline(file, line)) {
std::cout << line << std::endl;
}
file.close(); // Close the file
} else {
std::cerr << "Failed to open the file." << std::endl;
}
return 0;
}
3. Appending Data to a File:
#include <fstream>
int main() {
std::ofstream file("data.txt", std::ios::app); // Open file in append mode
if (file.is_open()) {
file << "This line will be appended." << std::endl;
file.close(); // Close the file
std::cout << "Data appended to file successfully." << std::endl;
} else {
std::cerr << "Failed to open the file." << std::endl;
}
return 0;
} Checking End-of-File (EOF):
1. Checking End-of-File (EOF):
#include <fstream>
int main() {
std::ifstream file("data.txt"); // Open file for reading
if (file.is_open()) {
std::string line;
while (!file.eof()) {
std::getline(file, line);
if (!file.eof()) {
std::cout << line << std::endl;
}
}
file.close(); // Close the file
} else {
std::cerr << "Failed to open the file." << std::endl;
}
return 0;
}
These examples demonstrate different file handling operations such as writing, reading, appending, and checking the end-of-file. Feel free to modify them or experiment with your own file handling scenarios.