If the file is opened for output operations and it already existed, its previous content is deleted and replaced by the new one.Īll these flags can be combined using the bitwise operator OR ( |). If this flag is not set, the initial position is the beginning of the file.Īll output operations are performed at the end of the file, appending the content to the current content of the file. Set the initial position at the end of the file. Where filename is a string representing the name of the file to be opened, and mode is an optional parameter with a combination of the following flags: In order to open a file with a stream object we use its member function open: An open file is represented within a program by a stream (i.e., an object of one of these classes in the previous example, this was myfile) and any input or output operation performed on this stream object will be applied to the physical file associated to it. This procedure is known as to open a file.
The first operation generally performed on an object of one of these classes is to associate it to a real file. This code creates a file called example.txt and inserts a sentence into it in the same way we are used to do with cout, but using the file stream myfile instead. basic file operations #include #include using namespace std