Another useful C++ ability is to read and write to files. That requires the standard C++ library called the fstream. Three new data types are introduced in fstream :
#include<iostream>
#include<fstream>
These classes are derived directly or indirectly from the classes istream and ostream. There are already used objects whose types are the classes: cin as an object of class istream and cout as an object of class ostream.
A file must be opened before being rad or written to. Either the ofstream or fstream object may be used to open a file for writing. So for example, consider a file "text.txt" containing the following text:
#include<iostream>
#include<fstream>
using namespace std;
int main(){
ofstream MyFile;
MyFile.open("test.txt");
Myfile<<"Some text. \n";
}
The above code creates an ofstream object called MyFile, and uses the open() function to open the "test.txt" file on the file system. As visible, the same stream output operator is used to write into the file. If the specified file does not exist, he open() function will create it automatically. When working with a file is finished, close it using the member function close() :
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream MyFile;
MyFile.open("test.txt");
MyFile << "Some text! \n";
MyFile.close();
}
Running this code will cause a "test.txt" file to be created in the directory of your project with "Some text!" written in it. The user also has the option to specify a path for the file, since it can be in a location other than that of the project. Provide the path for the file using the ofstream objects constructor, instead of calling the open function.
#include <fstream>
using namespace std;
int main() {
ofstream MyFile("test.txt");
MyFile << "This is awesome! \n";
MyFile.close();
}
As with the open function, a full path to the file located in some another directory can be provided. Under certain circumstances, like not having file permissions, the open function can fail. The is_open function checks whether the file is open and ready to be accessed.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream MyFile("test.txt");
if (MyFile.is_open()) {
MyFile << "This is awesome! \n";
}
else {
cout << "Something went wrong";
}
MyFile.close();
}
An optional second parameter of the open function defines the mode in which the file is opened. The following list shows the supported modes :
All these flags can be combined using the bitwise operator OR (|). For example, to open a file in write mode and truncate it, in case it already exists, use the following syntax:
ofstream outfile;
outfile.open("file.dat", ios::out | ios::trunc );
- ofstream : Output file stream that creates and writes information to files.
- ifstream : Input file stream that reads from files.
- fstream : General file stream, with both, ofstream and ifstream capacities that allow it to create, read, and write information to files.
#include<iostream>
#include<fstream>
These classes are derived directly or indirectly from the classes istream and ostream. There are already used objects whose types are the classes: cin as an object of class istream and cout as an object of class ostream.
A file must be opened before being rad or written to. Either the ofstream or fstream object may be used to open a file for writing. So for example, consider a file "text.txt" containing the following text:
#include<iostream>
#include<fstream>
using namespace std;
int main(){
ofstream MyFile;
MyFile.open("test.txt");
Myfile<<"Some text. \n";
}
The above code creates an ofstream object called MyFile, and uses the open() function to open the "test.txt" file on the file system. As visible, the same stream output operator is used to write into the file. If the specified file does not exist, he open() function will create it automatically. When working with a file is finished, close it using the member function close() :
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream MyFile;
MyFile.open("test.txt");
MyFile << "Some text! \n";
MyFile.close();
}
Running this code will cause a "test.txt" file to be created in the directory of your project with "Some text!" written in it. The user also has the option to specify a path for the file, since it can be in a location other than that of the project. Provide the path for the file using the ofstream objects constructor, instead of calling the open function.
#include <fstream>
using namespace std;
int main() {
ofstream MyFile("test.txt");
MyFile << "This is awesome! \n";
MyFile.close();
}
As with the open function, a full path to the file located in some another directory can be provided. Under certain circumstances, like not having file permissions, the open function can fail. The is_open function checks whether the file is open and ready to be accessed.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream MyFile("test.txt");
if (MyFile.is_open()) {
MyFile << "This is awesome! \n";
}
else {
cout << "Something went wrong";
}
MyFile.close();
}
An optional second parameter of the open function defines the mode in which the file is opened. The following list shows the supported modes :
All these flags can be combined using the bitwise operator OR (|). For example, to open a file in write mode and truncate it, in case it already exists, use the following syntax:
ofstream outfile;
outfile.open("file.dat", ios::out | ios::trunc );
Reading from a File :
Information can be read from a file using an ifstream or fstream object.
#include <iostream>
#include <fstream>
using namespace std;
int main () {
string line;
ifstream MyFile("test.txt");
while ( getline (MyFile, line) ) {
cout << line << '\n';
}
MyFile.close();
}
#include <fstream>
using namespace std;
int main () {
string line;
ifstream MyFile("test.txt");
while ( getline (MyFile, line) ) {
cout << line << '\n';
}
MyFile.close();
}
The getline function reads characters from an input stream and places them into a string.
Comments
Post a Comment