www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons Lesson 33: Output File Handling Bookmark and Share
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
Lesson 28
Lesson 29
Lesson 30
Lesson 31
Lesson 32
Lesson 33
Lesson 34
Lesson 35
Lesson 36
Lesson 37
Lesson 38
Lesson 39
Lesson 40
Let’s talk about the output file handling. You can do several things with output files like, creation of a new file on the disk and writing data in it. Secondly, we may like to open an existing file and overwrite it in such a manner that all the old information is lost from it and new information is stored. Thirdly, we may want to open an existing file and append it in the end. Fourthly, an existing file can be opened and modified in a way that it can be written anywhere in the file. Therefore, when we open a file for output we have several options and we might use any one of these methods. All these things are related to the file-opening mode. The actual syntax of open function is:

open (filename, mode)

The first argument is the name of the file while the second will be the mode in which file is to be opened. Mode is basically an integer variable but its values are pre-defined. When we open a file for input, its mode is input file that is defined and available through the header files, we have included. So the correct syntax of file opening for input is:

myFile.open(“myfile.txt” , ios::in);

The 2nd argument ios::in associates myFile stream object with the “myfile.txt” for input. Similarly, for output files, there are different modes available. To open a file for output mode, ios::out is used. Here is the complete list of modes:

Mode Meaning
in Open a file or stream for extraction (input)
out Open a file or stream for insertion (output)
app Append rather than truncate an existing file. Each insertion(output) will be written to the end of the file
trunc Discards the file’s contents if it exists. (similar to default behavior)
ate Opens the file without truncating, but allows data to be written anywhere in the file
binary Treat the file as binary rather than text. A binary file has data stored in internal formats, rather than readable text format

If a file is opened with ios::out mode, a new file is created. However, if the file already exists, its contents will be deleted and get empty unless you write something into it. If we want to append into the file, the mode will be ios::app. When we write into the file, it will be added in the end of the file. If we want to write anywhere in the file, the mode is ios::ate. We can position at some particular point and can write there. It is like append mode. But in ‘ate mode’ we can write anywhere in the file. With the trunc mode, the file is truncated, it is similar to out mode.

Example Code

* This program writes into a txt file “myfileOut.txt” which contains the
* “Welcome to the C Tutorial”
#include <iostream.h>
#include <fstream.h>
ofstream outFile; // Handle for the input file
char outputFileName[] = "myFileOut.txt"; // The file is created in the
//current directory
char ouputText[100] = "Welcome to the C Tutorial"; // used to write into
//the file
outFile.open(outputFileName, ios::out); // Opening the file
// checking that file is successfully opened or not
if (!outFile)
cout << "Can't open input file named " << outputFileName << endl;
// Writing into the file
outFile << ouputText;

The file “myFileOut.txt”:
Welcome to the C Tutorial

NEXT>>>>>Lesson 34. Advanced File Handling

Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.