The concept of C + + files
To date, the input output we have discussed is based on a system-specified standard device (input device is keyboard, output device is a monitor). In practical applications, disk files are often used as objects. That is, reading data from a disk file and outputting the data to a disk file. Disk is the external memory of the computer, it can retain information for a long time, can read and write, can refresh rewrite, easy to carry, and thus widely used.
Files (file) is an important concept in programming. The so-called "file", generally refers to the collection of data stored on the external media. A batch of data is stored in the form of a file in an external medium (such as a disk, disc, and U disk). The operating system manages data in files, that is, if you want to find data on external media, you must first locate the file you specified by file name, and then read the data from that file. To store data on external media, you must first create a file (identified by the filename) before you can output data to it.
External memory files include disk files, CD-ROM files and U disk files. The most widely used is the disk file, for the description of convenience, the tutorial where the use of external memory files are represented by the disk file, in the program to the CD-ROM file and U disk files are used in the same way as the file.
For users, there are two broad categories of files that are commonly used, such as program files, such as source program Files (. cpp) for C + +, target files (. obj), executable files (. exe), and so on. A class is the data file, in which the program runs, it is often necessary to output some data (the final result or intermediate data) to the disk storage, and then input to the computer memory from the disk when needed. This disk file is the data file. The object of input and output in the program is the data file.
According to the organization of the data in the file, it can be divided into ASCII file and binary file. An ASCII file is also called a text file or a character file, each byte of which puts an ASCII code representing one character. A binary file, also known as an internal format file or a byte file, is the output of the data in memory to disk as it is stored in memory.
For character information, it is stored in ASCII code in memory, so the data form is the same regardless of whether the output is in ASCII file or binary file. But for numerical data, the two are different. For example, there is a long integer 100000, 4 bytes in memory, if directly output in the internal format, in the disk file for 4 bytes, if it converted to ASCII form output, it takes 6 bytes.
The data output in ASCII form corresponds to the character one by one, and a byte represents a character that can be displayed or printed directly on the screen. This way is easy to use, more intuitive, easy to read, easy to input and output characters. But generally takes up more storage space, and it takes conversion time (binary form and ASCII code conversion). Using the internal format (binary form) output value, you can save memory space, and do not need to convert time, but a byte does not correspond to a character, can not directly display the contents of the file. If some intermediate result data is temporarily stored in the disk file in the process of running the program, and then need to enter into the memory, then it is most suitable to save with binary file. If it is intended to be displayed and printed for reading, it should be exported in ASCII form. This is an ASCII file, and its contents can be viewed directly on the display.
C + + provides low-level I/O capabilities and advanced I/O capabilities. Advanced I/O features combine several bytes into a meaningful unit (such as integers, single-precision, double-precision, string, or user-defined types of data), and then enter and output in ASCII character form. For example, the data from memory to the display output, is an advanced I/O function, first of all in-memory data into ASCII characters, and then by integer, single precision number, double precision, and other forms of output. This type-oriented input and output is common in programs and is convenient for users. However, in the transmission of large capacity files due to data format conversion, slow, inefficient.
The so-called low-level I/O functions are input and output in bytes, not in the input and output data format conversion. This input and output is done in binary form. Typically used to transfer a batch of bytes between memory and devices. This input and output speed, high efficiency, the general bulk of the file transfer with unformatted conversion of I/O. But it will be inconvenient when used.
C + + File open and close
Here's how to turn on and off files on disk, and the files on other peripherals (U disk, CD, etc.) are the same.
Open File
The so-called open file is an image of the statement, as open the door can enter the room activities. Opening a file means doing the necessary preparatory work before the file reads and writes, including:
Associates a file stream object with a specified disk file so that the file flow flows to the specified disk file.
Specifies how the file works, such as whether the file is an input file, an output file, an ASCII file, or a binary file.
The above work can be achieved in two different ways.
1 Call the member function of the file stream open. Such as
Ofstream outfile; Defines the Ofstream class (output file stream Class) object outfile
outfile.open ("F1.dat", ios::out);//Make file flow associated with F1.dat file
Line 2nd is a member function that calls the output file stream open Disk file F1.dat and specifies that it is an output file, and the file stream object outfile f1.dat output data to the disk file. Ios::out is a type of I/O pattern that represents opening a file in output mode. Or simply put, at this point f1.dat is an output file that receives data from memory output.
The general form of the call member function Open is:
File Stream object. Open (disk filename, input and output mode);
A disk file name can include a path, such as "C:\new\\f1.dat", such as the default path, which defaults to the file under the current directory.
2 Specify parameters when defining a file flow object
A constructor with parameters is defined when declaring a file flow class, which includes the ability to open a disk file. Therefore, you can specify parameters when defining a file flow object and invoke the constructor of the file flow class to implement the ability to open the file. Such as
Ostream outfile ("F1.dat", ios::out);
Generally more use this form, more convenient. The function is the same as the open function.
Input and output methods are defined in the iOS class, they are enumerated constants, there are several options, see table
A few notes:
1 The new version of the I/O class library does not provide ios::nocreate and ios::noreplace.
2 each open file has a file pointer, the initial position of the pointer is specified by I/O mode, each read and write from the current position of the file pointer. Each time a byte is read, the pointer moves back one byte. When the file pointer moves to the end, it encounters the end of file eof (a file terminator also occupies a byte, with a value of-1), at which time the value of the member function EOF of the stream object is not 0 (typically set to 1), indicating that the file is finished.
3 can use "bit or" operator "|" Combine the input and output methods, as shown in the last 3 lines in table 13.6. You can also cite the following examples:
Ios::in | iOS:: Noreplace//Open an input file, if the file does not exist then return to open the failure of information
Ios::app | ios::nocreate//Open an output file, at the end of the file to write data, if the file does not exist, then return to open the failure of information
ios::out L ios::noreplace//Open a new file as output file, return open failed message if file exists
ios::in l ios::out I ios::binary//Open a binary file, readable and writable
But they cannot be combined in mutually exclusive ways, such as Ios::nocreate l ios::noreplace.
4 If the open operation fails, the return value of the Open function is 0 (false), and the value of the stream object is 0 if the file is opened in the form of a call to the constructor. You can then test whether the success is open. Such as
if (Outfile.open ("F1.bat", Ios::app) ==0)
cout << "open error";
Or
if (!outfile.open ("F1.bat", Ios::app))
cout << "open error";
Turn off disk files
After you have completed reading and writing to an open disk file, you should close the file. Close the file with the member function. Such as
Outfile.close (); Closes the disk file associated with the output file stream
The so-called shutdown, in effect, is to disassociate the disk file from the file stream, and the original settings are not working, so that the file can no longer be input or output through the file stream. You can then associate the file stream with other disk files and enter or output the new file through the file stream. Such as
Outfile.open ("F2.dat", ios::app|ios::nocreate);
The file flow outfile is associated with F2.dat and specifies how F2.dat works.