C++ file system. General principles of work. Examples. Open/close file

C++ file system. General principles of work. Examples. Open/close file


Contents


1. Connecting access to classes for organizing work with C++ file system

File Input/Output is part of the general C++ Input/Output system. In order for the program to use file Input/Output, the fstream module must first be connected

#include <fstream>

This module implements the main classes for working with the file system:

  • ifstream – used to organize input from a file stream;
  • ofstream – used to organize output to a file (file stream);
  • fstream – used to provide Input/Output.

 

2. Creating a file stream. How is a stream connected to a file? Function open()

To create a file stream, you need to declare an instance of one of the classes ifstream, ofstream or fstream

// declaring class instances for different types of streams
ifstream is; // input stream
ofstream os; // output stream
fstream fs; // input/output stream

The created stream is associated with the file using the open() function, which has various implementations in different classes.

In the ifstream class, the open() function has the following prototype

ifstream::open(const char* filename, ios::openmode mode = ios::in);

here

  • filename – the name of the file to open. The name can be full or abbreviated, for example, “myfile.txt”;
  • mode – way to open a file. This parameter takes the value ios::in, which is described in the openmode() function. A value of ios::in means that the file is opened for input only.

In the ofstream class, the open() function has the following prototype

ifstream::open(const char* filename, ios::openmode mode = ios::out || ios::trunc);

here

  • filename – name of the file to open;
  • mode – a parameter that sets how the file is opened. In this parameter, the value ios::out means that the file is opened for output. The value ios::trunc means that the previous contents of an existing file with the same name will be deleted, and the file length will become zero.

In the fstream class, the prototype of the open() function is as follows

ifstream::open(const char* filename, ios::openmode mode = ios::in || ios::out);

here

  • filename – file name;
  • mode – a parameter that sets how the file is opened. As can be seen from the value of the mode parameter, the file is opened for both writing and reading.

 

3. Ways to open a file. Function openmode()

There are several ways to open a file. The values that describe how to open the file are implemented in the openmode() function. The following are the values that may be returned by the openmode() function and an explanation for them:

  • ios::app – the recorded data is appended to the end of the file;
  • ios::ate – when you open the file, it searches for the end of the file. But writing to a file can be in any place of the file;
  • ios::binary – allows you to open a file in binary mode. There are two modes of opening a file: text and binary. By default, files are opened in text mode;
  • ios::in – the file is opened for input (reading from the file occurs);
  • ios::out – the file is opened for output;
  • ios::trunc – means that the previous contents of an existing file with the same name will be deleted and the file length will become zero. If you open the output stream using ofstream class all the contents of the file with the same name will be erased.

Ways to open a file can be combined with each other using the | (bitwise OR operation).

Example. Ways to open a file.

// combining values that determine how the file is opened
// 1. Opening a file for output (writing to a file)
ofstream outFile;
outFile.open("myfile.txt", ios::out);

// 2. Opening a file for output - a simplified version (writing to a file)
outFile.open("myfile.txt");

// 3. Opening a file for input (reading from a file)
ifstream inFile;
inFile.open("myfile.txt", ios::in);

// 4. Opening a file for input - a simplified version (reading from a file)
inFile.open("myfile.txt");

 

4. Function is_open(). Checking the opening of a file. Example

The is_open() function is designed to check if a file is closed or not. This function is implemented in the ifstream, ofstream, and fstream classes.

The function has the following prototype:

bool is_open();

The function returns true if the stream is associated with an open file. Otherwise, the function returns false.



Example. How to open a file for an os object of the ofstream class is demonstrated. If the file is open, then this file is closed.

ofstream os; // declare an object of class ofstream
string filename = "myfile.txt";

// attempt to open file
os.open(filename);

if (!os.is_open())
{
  // file is not opened
  cout << "File is not opened." << endl;
}
else
{
  // display message
  cout << "File is open. Working with file." << endl;
  cout << "Closing file." << endl;

  // close the file
  os.close();
}

 

5. Function close(). Close the file. Example

To close the file, you need to call the close() function. The following shows opening and closing a file for an ifstream

ifstream is; // declare an instance of the ifstream class
is.open("myfile.txt"); // open file for input (reading from file)
is.close(); // close the file

 

6. Examples of opening/closing a file with the open() function. Checking if a file is open using the is_open() function

You can open a file in one of two ways:

  • using the open() function;
  • using the class constructor (see examples below).

If the file is open, then you can implement work with this file. It is possible that the file will not be opened after calling the f_open() function. In this case, the program must implement the appropriate check.

The correctness of opening a file can be implemented in one of the following two ways:

  • with the help of the corresponding checking using the conditional branch operator if;
  • using the function is_open().

Example 1. Opening a file for output.

#include <iostream>
#include <fstream>
using namespace std;

// Open/close file - class ofstream
void OpenCloseFileOutput()
{
  ofstream outFile;
  outFile.open("myfile3.txt", ios::out);

  if (outFile)
  {
    // work with file
    // ...
    outFile.close();
  }
}

Example 2. Opening a file for input

// Open, close file - class ifstream
void OpenCloseFileInput()
{
  // opening a file using the constructor
  ifstream inFile("myfile6.txt",ios::in);

  if (inFile)
  {
    // Use the file
    // ...
    inFile.close();
  }
}

Example 3. Opening a file for output. Using the function is_open().

// is_open () function, opening a file to write to it
void DemoIsFileOpenOutput()
{
  ofstream os; // declare an object of class ofstream
  string filename = "myfile6.txt";

  // an attempt to open a file
  os.open(filename);

  if (!os.is_open())
  {
    // file is not open
    cout << "File is not opened." << endl;
  }
  else
  {
    // Display message
    cout << "File is opened. Working with file." << endl;
    cout << "Close the file. " << endl;

    // Work with file
    // ...

    // Close the file
    os.close();
  }
}

Example 4. Opening a file for input (reading). Using the is_open() function.

// opening a file for input from it
void DemoIsFileOpenInput()
{
  ifstream is; // declare an ifstream class object
  string filename = "myfile6.txt";

  // an attempt to open a file
  is.open(filename);

  if (!is.is_open())
  {
    cout << "File is not opened." << endl;
  }
  else
  {
    // display message
    cout << "File is opened. Working with file" << endl;
    cout << "Close the file." << endl;
    is.close();
  }
}

 


Related topics