Files are used for data persistencepermanent retention of large amounts of data.
The smallest data item that computers support is called a bit (short for "binary digit"a digit that can assume one of two values, 0 or 1).
Digits, letters and special symbols are referred to as characters.
The set of all characters used to write programs and represent data items on a particular computer is called that computer's character set.
Bytes are composed of eight bits.
Just as characters are composed of bits, fields are composed of characters. A field is a group of characters that conveys some meaning.
Typically, a record (i.e., a class in C++) is composed of several fields (i.e., data members in C++).
At least one field in a record is chosen as a record key to identify a record as belonging to a particular person or entity that is distinct from all other records in the file.
In a sequential file, records typically are stored in order by a record-key field.
A group of related files often are stored in a database.
A collection of programs designed to create and manage databases is called a database management system (DBMS).
C++ views each file as a sequential stream of bytes.
Each file ends either with an end-of-file marker or at a specific byte number recorded in a systemmaintained, administrative data structure.
Header <fstream> includes the definitions for the stream class templates basic_ifstream (for file input), basic_ofstream (for file output) and basic_fstream (for file input and output).
For an ofstream object, the file-open mode can be either ios::out to output data to a file or ios::app to append data to the end of a file (without modifying any data already in the file).
The file-open mode ios::ate opens a file for output and moves to the end of the file. This is normally used to append data to a file, but data can be written anywhere in the file.
Existing files opened with mode ios::out are truncated (i.e., all data in the file is discarded).
By default, ofstream objects are opened for output.
The ofstream member function open opens a file and attaches it to an existing ofstream object.
An overloaded ios operator member functionoperator void*converts the stream to a pointer, so it can be tested as 0 (i.e., the null pointer) or nonzero (i.e., any other pointer value).
You can use the ofstream member function close to close the ofstream object explicitly.
Both istream and ostream provide member functions for repositioning the file-position pointer (the byte number of the next byte in the file to be read or written). These member functions are seekg ("seek get") for istream and seekp ("seek put") for ostream.
The seek direction can be ios::beg (the default) for positioning relative to the beginning of a stream, ios::cur for positioning relative to the current position in a stream or ios::end for positioning relative to the end of a stream.
Member functions tellg and tellp are provided to return the current locations of the "get" and "put" pointers, respectively.
Individual records of a random-access file can be accessed directly (and quickly) without the need to search other records.
The ostream member function write outputs a fixed number of bytes, beginning at a specific location in memory, to the specified stream. When the stream is associated with a file, function write writes the data at the location in the file specified by the "put" file-position pointer.
The istream member function read inputs a fixed number of bytes from the specified stream to an area in memory beginning at a specified address. If the stream is associated with a file, function read inputs bytes at the location in the file specified by the "get" file-position pointer.
string member function data converts a string to a non-null-terminated C-style character array.