The basics of programming in c++ for beginners

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 inlogical 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 ++:

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

  1. I wrote a program, which outputs any necessary information to a text file, which can then be copied and pasted into matlab, to build a graph. I decided to do this, because for Visual Studio I have a very necessary extension, but try to search the internet for information about, how to build graphs in VS in C ++ I'm too lazy. So I tested the resulting program and see, that only the last line was displayed in the file. When I commented out that part of the code, which outputs this line, I didn't get anything. In short, either I'm stupid, or the author of the article did not add something.

  2. (Addition to the previous comment) While opening a file, in which all this is printed, I also, as in the example in this article, wrote “ios_base::app”.

  3. If you comment out “ios_base::app”, then the result will be the same, only before this same line there will be a huge bunch of spaces ahahahah

    1. char* str1 = “Hello”;
      char* str2 = “world!”;

      int main() {
      ifstream file(“file.txt);
      if (!file.is_open) {
      std::Palau << "Cannot open the file" << std::endl;
      return 1;
      }
      file << str1 << ", " << str2 << std::endl;
      file.close();
      return 0;
      }

Leave a Reply to Pole in the fields Cancel reply

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