Detailed Print – Computer Sir Ki Class


Lost your password?

Don't have an account ?
Register (It's FREE) ×

Solved Problem#CPP#7479

Problem Statement - Empty File Creation

Creating an empty file using ofstream class

Learning Objectives

  • Learning to use ofstream class in its simplest form.
  • Learning to use the close() method of ofstream class.



Click to open popup

Run Output

File Created Successfully

Solved Problem Understanding

#include <iostream>
#include <fstream.h>   //This header file is required for data file handling classes
using namespace std;
int main(){

ofstream fout(“myfile.txt”);
For using creation of a file we use ofstream class (output file stream) with filename as parameter. This will create a file on the disk without any content (empty file). fout is the object name selected by us. we can give any name to this object.

cout<<“File Created Successfully”<<endl;
Displays that file has been created.

Here we are using the close method of ofstream along with the object name prefix.

return 0;}


  • This is the simplest way of creating a file, but for practical purposes we must check if the file has been created.
  • one can also make this program using fstream class which has capability of using both in input as well as output mode.

Common Errors

  • It is important to give file name as string literal or a null terminated pre-initialised char array.

Suggested Filename(s): filecrea.cpp,file-create.cpp