#include#include using namespace std; int main(){ //create file stream object fstream file; //open file in read-only mode file.open("example.txt", ios::in); //check if the file is open if(!file.is_open()){ cout<<"Unable to open file"<
This code uses the fstream object to open a file in read-only mode. It then reads the file line by line and prints each line to the console. The ios::in parameter is used to specify that the file should be opened in read-only mode.
Here's another example:#include#include using namespace std; int main(){ //create file stream object fstream file; //open file in write mode file.open("example.txt", ios::out); //check if the file is open if(!file.is_open()){ cout<<"Unable to open file"< This code opens a file in write mode using the fstream object. It then writes a string to the file and closes it. The ios::out parameter is used to specify that the file should be opened in write mode. The fstream library is part of the C++ standard library and does not require any additional package installation.