HOWTO · C++
How to Create a File in C++
Create or append to a file in C++ with standard streams or fopen, and check whether opening succeeded.
On this page
To create a file in C++, open it for output. For ordinary C++ output, std::ofstream is the simplest choice: it creates a missing file and, by default, replaces the contents of an existing file. Use std::fstream when you need input and output on the same stream, or fopen when working with the C standard I/O interface. Choose the mode based on whether existing contents should stay, be replaced, or receive more data.
Create or overwrite a file with std::ofstream
std::ofstream is an output file stream. Constructing it with a pathname opens the file; calling its open member function later does the same. Its default mode includes std::ios_base::out, which creates the file when needed and truncates an existing file. Check the stream before writing so that a path or permission problem is visible. The following standalone example writes a line, then reads the file back to show its contents:
#include <fstream>
#include <iostream>
int main() {
const char* path = "output.txt";
{
std::ofstream file(path);
if (!file) {
std::cout << "Could not open file\n";
return 1;
}
file << "First line\n";
}
std::ifstream input(path);
if (!input) {
std::cout << "Could not read file\n";
return 1;
}
std::cout << input.rdbuf();
return 0;
}
The output stream closes at the end of its scope, completing the write before the input stream reads the file. This example overwrites output.txt each time it runs. It does not create missing parent directories, so the parent of the chosen pathname must already be available.
Create or append to a file with std::fstream
Use std::fstream when a stream needs both input and output operations. Add std::ios_base::app to write at the end of the file. This mode creates a missing file, and each write goes to the end, preserving earlier contents. Here, a short first write gives the append operation existing content to retain:
#include <fstream>
#include <iostream>
int main() {
const char* path = "output.txt";
{
std::ofstream seed(path);
if (!seed) {
std::cout << "Could not open file\n";
return 1;
}
seed << "Start\n";
}
{
std::fstream file;
file.open(path, std::ios_base::app);
if (!file.is_open()) {
std::cout << "Could not append to file\n";
return 1;
}
file << "Added\n";
}
std::ifstream input(path);
if (!input) {
std::cout << "Could not read file\n";
return 1;
}
std::cout << input.rdbuf();
return 0;
}
open is a member function of std::fstream; it is not a built-in function or a std::open API. The stream’s is_open() member reports whether it is associated with a file. Checking it before writing prevents the program from treating a failed open as a successful append.
Create or open a file with fopen
The C standard I/O function fopen accepts a pathname and a mode string, and returns a FILE*. A null pointer means the file could not be opened, so check the result before calling fwrite or another operation on it. For example, mode w creates a missing file and truncates an existing one. The program below writes a line, checks the write and close results, then reads the file back and prints its contents:
#include <cstdio>
#include <iostream>
int main() {
const char* path = "output.txt";
FILE* file = std::fopen(path, "w");
if (file == nullptr) {
std::cout << "Could not open file\n";
return 1;
}
const char text[] = "Created with fopen\n";
const std::size_t written = std::fwrite(text, 1, sizeof(text) - 1, file);
if (written != sizeof(text) - 1) {
std::cout << "Could not write complete file\n";
std::fclose(file);
return 1;
}
if (std::fclose(file) != 0) {
std::cout << "Could not close file\n";
return 1;
}
FILE* input = std::fopen(path, "r");
if (input == nullptr) {
std::cout << "Could not read file\n";
return 1;
}
char buffer[128];
while (std::fgets(buffer, sizeof(buffer), input) != nullptr) {
std::cout << buffer;
}
if (std::ferror(input)) {
std::cout << "Could not read file\n";
std::fclose(input);
return 1;
}
if (std::fclose(input) != 0) {
std::cout << "Could not close input file\n";
return 1;
}
return 0;
}
Mode w+ also truncates, but permits reading and writing. Mode a creates a missing file and appends writes; a+ permits reading as well as appending writes. In contrast, r and r+ require an existing file. Call fclose when finished to release the C stream and report any close failure.
Choose a mode and verify the result
Choose an output mode according to what should happen to prior contents. The default std::ofstream mode uses std::ios_base::out and replaces existing data. Use std::ios_base::app when every write must go to the end. In C stdio, w replaces, while a appends. These writing modes create a file if its parent directory exists and is writable; they do not create directories.
For C++ streams, test the stream with if (!file) after opening, or use is_open() to check its association with a file. For fopen, compare the returned pointer with nullptr. An open check confirms that opening succeeded, not that every later write or close will succeed; check operation results when those failures matter. See the C++ open mode reference for the stream mode combinations and details.