files_inc++
files_inc++
files_inc++
1
FACULTY OF ELECTRONIC ENGINEERING TECHNOLOGY
Spring Term :2021-2022
Subject: CP II 4 th SEMESTER
The three objects, that is, fstream, ofstream, and ifstream, have the open() function defined in
them. The function takes this syntax:
open (file_name, mode);
Value Description
ios:: app The Append mode. The output sent to the file is appended to it.
ios::ate It opens the file for the output then moves the read and write control to file’s end.
ios::in It opens the file for a read.
ios::out It opens the file for a write.
ios::trunc If a file exists, the file elements should be truncated prior to its opening.
It is possible to use two modes at the same time. You combine them using the | (OR) operator.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream my_file;
my_file.open("my_file", ios::out);
if (!my_file) {
cout << "File not created!";
}
else {
cout << "File created successfully!";
my_file.close();
}
return 0;
}
2
FACULTY OF ELECTRONIC ENGINEERING TECHNOLOGY
Spring Term :2021-2022
Subject: CP II 4 th SEMESTER
#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream my_file;
my_file.open("my_file.txt", ios::out);
if (!my_file) {
cout << "File not created!";
}
else {
cout << "File created successfully!";
my_file << "Guru99";
my_file.close();
}
return 0;
}
while (1) {
my_file >> ch;
if (my_file.eof())
break;
}
my_file.close();
return 0;
}