C++ Tutorials


Writing Text Files

Tutorials > C++ > Writing Text Files

View Full Source


In most programs, you would like the ability to save data to be used at a later stage. This tutorial will explain how to store ascii(textual) data into files. I will explain the process in both C and C++. Remember that you should not use both together. I am placing them together to show you how each one works.

Contents of main.cpp :

#include <iostream>
#include <stdlib.h>

2 new header files need to be included. The fstream include is used for C++ file manipulation and the stdio.h header file is used for C file manipulation.

#include <fstream>
#include <stdio.h>

using namespace std;

int main()

For manipulating files in C, a pointer to a FILE structure is used. Our first step in writing to a text file is to create or open the file. This is achieved using the fopen function. The first parameter is the name of the file that you are wanting to write to. The second parameter specifies what mode you want to open the file in. We are wanting to write to the file and therefore a "w" is passed. Text mode is automatically used. An additional 'b' is placed after the 'w' for binary mode.

	// C File Writing
	FILE *outFile = NULL;

	outFile = fopen("cout.txt", "w");

If the file already exists, it will be overwritten. If the file does not exist, it will be created. If a value of NULL is returned from the fopen function, it means that the file could either not be created or the file could not be written to.

	if (!outFile)
		printf("Error creating cout.txt\n");
		return 1;

To send formatted text to a text file, the fprintf function can be used. This function works in the exact same way as the printf function except that it takes an additional parameter. The first parameter is the pointer to a FILE structure that is returned from the fopen function.

	fprintf(outFile, "Hello World\n");
	fprintf(outFile, "Tutorial : %d\n", 37);

An alternative of writing strings to a text file is to use the fputs function. This function simply writes a string to the file. No additional parameters can be passed to the function ie. using the % identifiers. The first parameter is the FILE pointer and the second is the character string.

	fputs("fputs\n", outFile);

If you wish to place a single character, you can use the fputc function. It works in the exact same way as the fputs function except that it takes a character instead of a character string.

	fputc('.', outFile);

One must always make sure that they close a file after reading from or writing to it. This ensures that all data written or read is saved. The fclose function takes one parameter being the FILE pointer and it closes the file. Make sure that you place an fclose function call whenever you have an fopen call and that no matter what paths your program may take, all files should be closed.


C++ file writing uses the idea of file streams. To create a stream, we create an ostream (output stream) object.

	// C++ File Writing
	ofstream fout;

An open method is available to open the file for writing. If the file does not exist, it will be created. Parentheses could also have been placed after the fout above containing the name of the file. This would automatically open the file."c++out.txt");

Another method is the good method. This method returns true if the file can be written to or if the file was created successfully. If any problem has occurred, false will be returned.

	if (!fout.good())
		cout << "Error creating c++.out.txt";
		return 1;

The newly created fout object can be used in the same way as the cout object. The only difference is that the fout object sends the data to a text file.

	fout << "Hello World" << endl;
	fout << "Tutorial : " << 37 << endl;

	fout << "fputs" << endl;

If you would like to add one character to the file, you can do so using the redirection operators as above or you could use the put method. This method takes one parameter which is the character you want to add to the file.


As we spoke about earlier, it is always necessary to close a file after you have finished working with it. A close method is available to achieve this.



	return 0;

Congratulations. You should now be able to save data from your programs in text files. This is extremely useful and allows you to store data while the program is not running. The next tutorial will deal with reading data back out from a text file.

Please let me know of any comments you may have : Contact Me

Source Files : Visual Studio Dev-C++ Unix / Linux

< Tutorial 36 - Function Overloading Tutorial 38 - Reading Text Files >

Back to Top

All Rights Reserved, © Zeus Communication, Multimedia & Development 2004-2005

Read the Disclaimer