Example
Projects
C++ Graphics
C++ Forum
Contact Us
Tutorial contents
Introductions
C++ Basics
Program Structure
Variable: Data types
Constants
Operators
Basic Input/Output
Control Structures if....else statement while loop do...while loop for loop switch break, continue goto, exit statement
Function
Compound Structure
Array
Pointer
Data Structure
OOPs
Class
Inheritance
Polymorphism
Advanced
File Handling
C++ Graphics
Extra
C++ Examples
C++ Projects
DATA / FILE HANDLING IN C++
Before getting into this section, it is recommended that you have a proper understanding of Text file and Binary File. If any of both seems strange to you we give short information about these files:-
Text file. The text file stores the data in ASCII character. Each line of text is terminated with a special character known as EOL (End of Line) character or delimiter character in text files. When this EOL character is read or written, certain internal translations take place.
Binary file. The Binary File stores the data in the same format as it is held in memory. No delimiters are used for a line and no translations occur here in binary files.
The following stream classes can be used in C++ to working with files: ofstream: Write on files ifstream: Read from files fstream: Both read and write from/to files. OPENING FILE
To open a file we use open() command as following: ofstream cppfile; cppfile.open(“filename”,mode); In the above line: ofstream cppfile:
By using ofstream we declared a file variable cppfile who used to access file. cppfile.open (“filename”,mode): You can see that we use the file variablecppfile with open() to open a file. filename is a file which to be open and modecan be the following :
File mode
Uses
ios::app
Append to end of file ios::ate go to end of file on opening ios::binary file open in binary mode ios::in open file for reading only ios::out open file for writing only