C++ Program to Create a File Last Updated : 17 Sep, 2024 Comments Improve Suggest changes Like Article Like Report 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 open that text file in writing mode. If the file is not present then it creates a new text file. Now check if the file does not exist or not created then return false otherwise return true. Follow the below steps: Declare an ofstream object named file for output file operations.Use file.open("fileName") to create and open a file in write mode.Verify if the file was successfully opened using file.is_open(). If the file wasn't created, output an error message and return a non-zero value.If the file opens successfully, print "File created successfully."Use file.close() to close the file and free up system resources.Below is the program to create a file: CPP #include <fstream> #include <iostream> using namespace std; int main(){ //using ofstream for output file operations. ofstream file; // Opening file "Gfg.txt" in write mode. file.open("Gfg.txt"); // Check if the file was successfully created. if (!file.is_open()) { cout << "Error in creating file!" << endl; // Return a non-zero value to indicate an error. return 1; } cout << "File created successfully." << endl; // Close the file to free up resources. file.close(); return 0; } OutputFile created successfully. //File named Gfg.txt is created Comment More infoAdvertise with us Next Article C++ Program to Create a File B bansal_rtk_ Follow Improve Article Tags : C++ Programs C++ cpp-file-handling Practice Tags : CPP Similar Reads 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 Create Directory or Folder with C/C++ Program Problem: Write a C/C++ program to create a folder in a specific directory path. This task can be accomplished by using the mkdir() function. Directories are created with this function. (There is also a shell command mkdir which does the same thing). The mkdir() function creates a new, empty director 2 min read C++ Program to Make a File Read-Only 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, ifstre 2 min read How to Delete a File in C++? C++ file handling allows us to manipulate external files from our C++ program. We can create, remove, and update files using file handling. In this article, we will learn how to remove a file in C++. Delete a File in C++ To remove a file in C++, we can use the remove() function defined inside the 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 Like