The
In C++, files are opened using the open() function and closed using the close() function. The fstream class provides these functionalities.
#include <fstream>
#include <iostream>
using namespace std;
int main() {
fstream file;
file.open("example.txt", ios::out);
if (file.is_open()) {
cout << "File opened successfully." << endl;
file.close();
} else {
cout << "Failed to open file." << endl;
}
return 0;
}
The ofstream class is used for writing to files. It provides an easy interface to output data to a file.
#include <fstream>
#include <iostream>
using namespace std;
int main() {
ofstream file("output.txt");
if (file.is_open()) {
file << "Hello, World!" << endl;
file.close();
cout << "Data written to file." << endl;
} else {
cout << "Failed to open file for writing." << endl;
}
return 0;
}
The ifstream class is used for reading from files. It allows you to input data from a file into your program.
#include <fstream>
#include <iostream>
using namespace std;
int main() {
ifstream file("input.txt");
string line;
if (file.is_open()) {
while (getline(file, line)) {
cout << line << endl;
}
file.close();
} else {
cout << "Unable to open file for reading." << endl;
}
return 0;
}
To append data to an existing file, open the file in append mode using ios::app. This mode ensures that new data is added to the end of the file without overwriting existing content.
#include <fstream>
#include <iostream>
using namespace std;
int main() {
ofstream file("output.txt", ios::app);
if (file.is_open()) {
file << "Appending this line." << endl;
file.close();
cout << "Data appended to file." << endl;
} else {
cout << "Failed to open file for appending." << endl;
}
return 0;
}
Binary files store data in a format that is not human-readable but is efficient for computer processing. Use ios::binary mode for binary file operations.
#include <fstream>
#include <iostream>
using namespace std;
int main() {
// Writing binary data
ofstream outFile("data.bin", ios::binary);
int num = 12345;
outFile.write(reinterpret_cast<char*>(&num), sizeof(num));
outFile.close();
// Reading binary data
ifstream inFile("data.bin", ios::binary);
int readNum;
inFile.read(reinterpret_cast<char*>(&readNum), sizeof(readNum));
inFile.close();
cout << "Read number: " << readNum << endl;
return 0;
}
The is_open() function checks whether a file is successfully opened. It returns true if the file is open, otherwise false.
#include <fstream>
#include <iostream>
using namespace std;
int main() {
ifstream file("test.txt");
if (file.is_open()) {
cout << "File is open." << endl;
} else {
cout << "File is not open." << endl;
}
file.close();
return 0;
}
The fail() function checks if a file operation failed, while bad() checks for serious errors. These functions help in robust error handling.
#include <fstream>
#include <iostream>
using namespace std;
int main() {
ifstream file("nonexistent.txt");
if (file.fail()) {
cout << "File operation failed." << endl;
} else {
cout << "File operation succeeded." << endl;
}
file.close();
return 0;
}
The seekg() and seekp() functions are used to set the position of the next character to be read or written in a file. These functions are essential for random file access.
#include <fstream>
#include <iostream>
using namespace std;
int main() {
fstream file("example.txt", ios::in | ios::out | ios::binary);
file.seekp(5); // Move write pointer to the 5th position
file << "X";
file.seekg(0); // Move read pointer to the start
char ch;
file.get(ch);
cout << "First character: " << ch << endl;
file.close();
return 0;
}
The sync() function is used to synchronize the associated buffer with the file. This ensures that all pending output operations are completed before the program proceeds.
#include <fstream>
#include <iostream>
using namespace std;
int main() {
fstream file("sync_example.txt", ios::out);
file << "Data before sync.";
file.flush(); // Alternatively, you can use file.sync();
cout << "Data synchronized to file." << endl;
file.close();
return 0;
}
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies