How to Create a New File in C++


C++


In this article, we show how to create a new fiel in C++.

C++ is a full, complete programming that language that of course can carry out operating system level tasks, such as creating files on your computer.

This file can be a text file (.txt), an HTML file (.html), a rich text format file (.rtf), etc.

C++ can even create .doc files, which is the file for Microsoft Word from 1997-2003. However, C++ does not create readable .docx files, which is the latest file extension for Microsoft Word due to the complexities of the later model.

The standard C++ library provides the functionality for working with files.

For each file that you work with, a filestream object must first be created. This will either be an "ofstream" (output filestream) for writing data to the file, or an "ifstream" (input filestream) for reading data from a file.

Creating a Blank File

So let's now create a basic blank text file with C++.

We will later show how to create a file with contents inside.

So to create a blank text file in C++, we use the following code shown below.



So let's now go over the code.

So in order to be able to work with files, we must include the C++ standard library. This is done using the line, #include

In order to use the cout function, which we use in our program in case there is an error opening the file, we use the iostream library.

We then have our main() function.

The first thing we want to do is create our ofstream writer object, which is an output file. It is an output file because we are creating a file, not opening an existing file to read from it.

We then check to see if this writer object exists. If not, we output an error message and return -1.

You then have to make sure that you close the writer object after all write operations are finished. This is done through the line, writer.close()

We then end the program by returning 0.

This program will create a file named "file1.txt" within the directory that the current project is located in (within the project folder in your workspace). If you want to place it in some other location, then you must specify the full path to where you want the file created.

Creating a File with Contents

So above we showed how to create a blank file. Now we show how to create a file with contents.

We will create a simple text file with the contents "Hello"

This is shown in the code below.



Just like before, we need to use the library to work with files.

We also need the library to output an error message in case the file isn't successfully created.

Since we are writing string contents to the file, we need to use the library.

We then create our string stating, "Hello" in the variable, contents.

We then write the contents of the variable to the writer object.

We then close the writer object.

A file named "file2.txt" should be created in the current working project directory that has the content "Hello" in it.

Remember, with the library in C++, you can create many different types of files, including .rtf files, .doc files, .html files, .php files, etc. It is very versatile.

And this is how to create a new file in C++, either blank or with contents.


Related Resources

How to Receive Input from a User in C++

HTML Comment Box is loading comments...