Working with files in C ++. Part 2 – Библиотека fstream.




class ofstream

Back class ifstream, which we reviewed earlier, and is designed to write to a file.

As in ifstream, to open or create a file using a constructor or method open().

or designer:

Parameter ios_base::app indicated, if you want to append to the end of an existing file. For example when the program keeps a log of their work

For checking a file open meets all the same is_open()

The principle is the same. You can check whether a file is opened, using in logical expression very variable file:

Statement <<

Forwards the formatted output to a file. The principle is the same, that of the analog in iostream.

It is intended to be displayed in text files. Controlled formatting operations such as width() or setf(). Their counterparts are fully methods of the same name iostream.

The sequence of output variables as indicated from left to right: The first variable will be output, listed closest to fillet, then following it.

operator endl

Similarly, the operator of iostream manufactures transfer record of the carriage to a new line in a text file.

method write

It used in binary files for writing the memory block (byte array) in the file as they are. Any variable is also a array bytes, rather it can be seen as. Accordingly, this method writes to the file its machine representation (the kind of the way it looks in the memory).

This method takes two parameters: Pointer to a block of data and the number of bytes, that this unit takes. In the example, the string takes strlen() bytes, whole sizeof() (which will give 4 32-bit integer and for OSes 8 for real).

Once again I should emphasize the, that unlike the formatted output operator <<, method write() It does not display data in text form.

method close

Closes file method close(). for files, writable, as opposed to reading files, closing a file – mandatory ritual. Unclosed file can not obtain the data. This effect can occur due to the buffering itself OSes, when data, discharged into the file, They are stored in fact in mind and just do not come to a file. The operating system itself decides, when the data is time to merge.

Such “deferred” plums called “commit” (from the Latin commit). By the way this effect very well use a database management system, where to insert records fall into the storage memory (called transaction). It was only after a special command en masse written to the database file itself. Method close() just an example of a team closing a transaction with the file.

It is worth mentioning just in case, that if you want to commit to produce data without closing the file itself, you need to use the method flush()

These deposits will go on the record in the file, but it will still be open for entries. This method is not often used, but useful to know about it.

width formatting methods, precision

As in iostream, for the beautiful layout of data in data formatting file can be used to display operator << .

width() indicates the width in characters, which will be laid in the displayed value, and precision() the number of digits of the fractional part of a real. The simplest example: output table to a text file the values ​​of the trigonometric functions:

 

positioning methods seekp, tellp

To move a file as in the case of ifstream There is a function of the position of the permutation. it is called seekp() and receives the same parameters as described above for seekg().

To get the current position of a similar feature in bytes from the beginning of the file tellp().

Videos about working with files in C ++:

Newsletter of programming:

Working with files in C ++. Part 2 – Библиотека fstream.
5 (100%) 1 vote

Leave a Reply

Your email address will not be published. Required fields are marked *