

View Image file1.open( "c:\\config.sys" ,ios::binary|ios:: in ,0) Such as ios::out|ios::binaryĬan use "or" or "+" to connect the above attributes, such as 3 or 1|2 is read-only and hidden attributes open a file.įor example: Open the file c:\config.sys in binary input mode You can use "or" to connect the above attributes. Ios::trunc: If the file exists, set the file length to 0 and Ios::noreplace: does not overwrite the file, so when opening the file, if the file exists, it fails. Ios::nocreate: the file is not created, So when the file does not exist, it fails to open. Ios::out: the file is opened as output (the memory data is output to the file) Ios::in: the file is opened as input (file data is input to the memory) The difference between the two methods is shown in the previous article Ios::binary: Open the file in binary mode, the default mode is text mode. Ios::ate: After the file is opened, locate to the end of the file, ios:app contains this attribute View Image void open( const char * filename, int mode, int access) įilename: the name of the file to be openedįile to open the way to open the file is defined in the class ios (the base class of all streaming I/O classes), and the commonly used values are as follows: In the fstream class, there is a member function open(), which is used to open the file. Let's take this kind of file operation process one by one. Therefore, to manipulate files in this way, you must add the header file fstream.h. In C++, file operations are implemented through the stream subclass fstream (file stream). For example, the system has a default standard input stream (cin), which generally refers to the keyboard, so cin>x means to read data of a specified type (that is, the type of variable x) from the standard input stream. The stream class has two important operators:

All I/O is based on this "stream" class, including the file I/O we want to know about. Ofstream is from memory to hard disk, ifstream is from hard disk to memory, in fact, the so-called stream buffer is the memory space
