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.
Below is the program to create a file:
// C++ implementation to create a file #include <bits/stdc++.h> using namespace std; // Driver code int main() { // fstream is Stream class to both // read and write from/to files. // file is object of fstream class fstream file; // opening file "Gfg.txt" // in out(write) mode // ios::out Open for output operations. file.open("Gfg.txt",ios::out); // If no file is created, then // show the error message. if(!file) { cout<<"Error in creating file!!!"; return 0; } cout<<"File created successfully."; // closing the file. // The reason you need to call close() // at the end of the loop is that trying // to open a new file without closing the // first file will fail. file.close(); return 0; } |
Output:

Recommended Posts:
- C program to copy contents of one file to another file
- Write a C program that displays contents of a given file like 'more' utility in Linux
- C Program to count number of lines in a file
- C Program to print contents of file
- C Program to merge contents of two files into a third file
- C program to delete a file
- C++ program to print unique words in a file
- C++ program to read file word by word
- C++ program to append content of one text file to another
- Program to delete a line given the line number from a file
- Javascript | Program to read text File
- C Program to find size of a File
- C++ program to modify the content of a Binary File
- C++program to delete the content of a Binary File
- C program to reverse the content of the file and print it
- Create Directory or Folder with C/C++ Program
- Program to create Custom Vector Class in C++
- fopen() for an existing file in write mode
- Implement your own tail (Read last n lines of a huge file)
- Four File Handling Hacks which every C/C++ Programmer should know
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

