- C++ Programming Basics
- C++ Tutorial
- C++ Environment Setup
- C++ Character Set
- C++ Keywords
- C++ Identifiers
- C++ Constants
- C++ Punctuators
- C++ Program Structure
- C++ Basic Syntax
- C++ Comments
- C++ Basic Programs
- C++ Input Output Operator
- C++ Input Output Stream
- C++ Type & Variable
- C++ Data Types
- C++ Data Type Modifiers
- C++ Variables
- C++ Variable Types
- C++ Variable Scope
- C++ Storage Classes
- C++ Formatting Output
- C++ Operators
- C++ Operators
- C++ Type Conversion
- C++ Numbers
- C++ Assignment Operator
- C++ Shorthand
- C++ Flow of Control
- C++ Statements
- C++ Flow Control
- C++ Decision Making
- C++ if if-else if-else-if switch
- C++ Loops
- C++ for while do-while Loop
- C++ break continue goto
- C++ Standard Library
- C++ Standard Library
- C++ Header Files
- C++ Character String
- C++ Mathematical Functions
- C++ Functions
- C++ Functions
- C++ Function Types
- C++ Function Prototype
- C++ Function Call
- C++ Function Return
- C++ Friend Function
- C++ Scope Rules
- C++ Arrays & Strings
- C++ Arrays
- C++ One Dimensional Arrays
- C++ Two Dimensional Arrays
- C++ Strings
- C++ Data Structure
- C++ Data Structure
- C++ Access Structure Member
- C++ Nested Data Structure
- C++ Structure Array
- C++ Pass Structure to Function
- C++ typedef
- C++ #define
- C++ Programming Pointers
- C++ Pointers
- C++ Memory Map
- C++ Free Store
- C++ Declare Initialize Pointers
- C++ Dynamic Memory Allocation
- C++ Pointers & Arrays
- C++ Pointers & Const
- C++ Pointers & Functions
- C++ Pointers & Structures
- C++ Objects as Function Arguments
- C++ Pointers & Objects
- C++ References
- C++ File Handling
- C++ File Handling
- C++ File Streams
- C++ Data Files
- C++ Opening & Closing Files
- C++ Steps to Process Files
- C++ Change Stream Behaviour
- C++ Sequential I/O Operations
- C++ Detecting EOF
- C++ File Pointers Random Access
- C++ Binary Files Operations
- C++ Error Handling
- C++ Object Oriented
- C++ Object Oriented
- C++ Function Overloading
- C++ Classes & Objects
- C++ Constructors & Destructors
- C++ Inheritance
- C++ Encapsulation
- C++ Polymorphism
- C++ Data Abstraction
- C++ Interfaces
- C++ Programming Advance
- C++ Linked Lists
- C++ Stacks
- C++ Queues
- C++ Date Time
- C++ Preprocessors
- C++ Exception Handling
- C++ Namespaces
- C++ Dynamic Memory
- C++ Multithreading
- C++ Templates
- C++ Signal Handling
- C++ Web Programming
- C++ Programming Examples
- C++ Programming Examples
- C++ Programming Test
- C++ Programming Test
- Give Online Test
- All Test List
C++ Change the Behaviour of Streams
We already have talked about various file modes. But now let's talk about their role in controlling a stream's behaviour.
When you open a file (i.e., link it with the stream) using constructor, the stream is activated in its default mode. The default mode for input type stream (ifstream type) is ios::in and for output type stream (ofstream type), it is ios::out.
The default behaviour of ifstream type stream (upon opening file) allows users to read contents from the file. If the filemode is ios::in only (e.g., fin.open("abc.dat", ios::in)) then reading is performed on a text file and if the filemode is ios::in | ios::binary (e.g., fin.open("abc.dat", ios::in | ios::binary)) then reading is performed on a binary file.
Changing the Default Behaviour of ofstream Type Stream
The default behaviour of an ofstream type stream (ios::out file mode), upon opening a file, is that it create the file if the file doesn't yet exist and it truncates the file (i.e., deletes its previous contents) if the file already exists. If you want to change this default behaviour, then you can do it by specifying any of the following file modes with open() function :
Modes | Description |
---|---|
ios :: app | to retain the previous contents of the file and to append to the end of existing file contents, rather than truncating them. |
ios :: ate | to place file-pointer at the end of the file, but you can write data anywhere in the file. |
ios :: trunc | it causes the existing file to be truncated. |
ios :: nocreate | if file doest not exist, this filemode ensures that no file is created and hence open() fails. However, if the file exists, it gets opened. |
ios :: noreplace | if the file does not exist, a new file gets created but if the file already exists, the open() fails. |
Example
Here is an example program:
/* C++ Changing the Behaviour of Streams */ #include<fstream.h> #include<conio.h> #include<stdlib.h> void main() { char fname[20], ch; char ans='y'; int rollno; float marks; ofstream fout; clrscr(); cout<<"Enter file name: "; cin.get(fname, 20); cin.get(ch); fout.open(fname, ios::app); while(ans=='y' || ans=='Y') { cout<<"\nEnter rollno: "; cin>>rollno; cout<<"Enter marks: "; cin>>marks; fout<<rollno<<"\n"<<marks<<"\n"; cout<<"\nWant to enter more ? (y/n).."; cin>>ans; } cout<<"\nPress any key to exit...\n"; getch(); }
Here is the sample run of the above C++ program:

More Examples
Here are some more examples listed, that you can go for:
- Read a File
- Write to File
- Read & Display File
- Copy File
- Merge two File
- List Files in Directory
- Delete File
- Encrypt and Decrypt Files
« Previous Tutorial Next Tutorial »