C++ Program to Make a File Read-Only Last Updated : 28 Jul, 2022 Comments Improve Suggest changes Like Article Like Report Here, we will build C++ Program to Make a File Read-Only using 2 approaches i.e. Using ifstreamUsing fstreamC++ programming language offers a library called fstream consisting of different kinds of classes to handle the files while working on them. The classes present in fstream are ofstream, ifstream, fstream. 1. Using "ifstream"The output of the below code consists of data present in the specified "Geeks for Geeks.txt" file. ifstream class is used to read the file and doesn't support writing data into the file. i.e. making files available to read-only. The created input stream "ifstream in" specifies the file to be opened in reading mode. C++ // C++ program to make the file // in read-only mode using ifstream #include <fstream> #include <iostream> using namespace std; int main() { // creating an input stream ifstream in; // opening a file in read mode using in in.open("Geeks for Geeks.txt"); if (!in) cout << "No file found"; else { char c; while (1) { in >> c; if (in.eof()) break; cout << c; } } in.close(); return 0; } Output: Geeks_for_Geeks2. Using "fstream"The fstream class is used to read the file and also enables writing data into the opened file. Here in the below code, we opened a file in read mode by specifying "ios::in" in the open method and displayed the data present in the file on the successful opening of a file. C++ // C++ program to make the file // in read-only mode using fstream #include <fstream> #include <iostream> using namespace std; int main() { fstream readFile; // opening a file in read mode readFile.open("Geeks for Geeks.txt", ios::in); if (!readFile) cout << "No such file exist"; else { char c; while (1) { readFile >> c; if (readFile.eof()) break; cout << c; } } // closing the file readFile.close(); return 0; } Output: Geeks_for_Geeks Comment More infoAdvertise with us Next Article C++ Program to Make a File Read-Only A akhilvasabhaktula03 Follow Improve Article Tags : C++ Programs C++ C++ File Programs Practice Tags : CPP Similar Reads How to Read From a File in C++? Reading from a file means retrieving the data stored inside a file. C++ file handling allows us to read different files from our C programs. This data can be taken as input and stored in the program for processing. Generally, files can be classified in two types:Text File: Files that contains data i 4 min read C++ Program to Create a File Problem Statement:Write a C++ program to create a file using file handling and check whether the file is created successfully or not. If a file is created successfully then it should print "File Created Successfully" otherwise should print some error message. Approach:Declare a stream class file and 2 min read C++ Program to Create a Temporary File Here, we will see how to create a temporary file using a C++ program. Temporary file in C++ can be created using the tmpfile() method defined in the <cstdio> header file. The temporary file created has a unique auto-generated filename. The file created is opened in binary mode and has access m 2 min read C++ Program to Copy One File into Another File To copy the text/contents of one file to another file, we should know the basics of reading and writing a text file in C++. To copy the file using C++, we read the contents of the source file and write it into the destination file. Steps to copy one file to another in C++: Create objects of ifstream 2 min read How to Read a File Using ifstream in C++? In C++, we can read the contents of the file by using the ifstream object to that file. ifstream stands for input file stream which is a class that provides the facility to create an input from to some particular file. In this article, we will learn how to read a file line by line through the ifstre 2 min read Like