C + + advanced tutorial: C + + files and streams

Time:2022-5-18

So far, we have used the # iostream # standard library, which provides # CIN # and # cout # methods for reading streams from standard input and writing streams to standard output, respectively.

This tutorial describes how to read and write streams from and to files. This requires the use of another standard library in C + +, fstream, which defines three new data types:

 

Open file

Before reading information from or writing information to a file, you must first open the file. Both OFSTREAM # and # fsstream # objects can be used to open a file for write operations. If you only need to open a file for read operations, use the # ifstream # object.

The following is the standard syntax for the open () function, which is a member of fsstream, ifstream, and OFSTREAM objects.

void open(constchar*filename, ios::openmode mode);

Here, the first parameter of the open () member function specifies the name and location of the file to be opened, and the second parameter defines the mode in which the file is opened.

 

You can combine the above two or more modes. For example, if you want to open a file in write mode and want to truncate the file in case it already exists, you can use the following syntax:

ofstream outfile;

outfile.open(“file.dat”, ios::out| ios::trunc );

Similarly, if you want to open a file for reading and writing, you can use the following syntax:

ifstream afile;

afile.open(“file.dat”, ios::out| ios::in);

Close file

When the C + + program terminates, it will automatically close, refresh all streams, release all allocated memory, and close all open files. But programmers should make it a good habit to close all open files before the program terminates.

The following is the standard syntax for the close () function, which is a member of fsstream, ifstream, and OFSTREAM objects.

void close();

write file

In C + + programming, we use the stream insertion operator (< <) to write information to the file, just as we use this operator to output information to the screen. The only difference is that here you use the , OFSTREAM , or , fstream , object instead of the , cout , object.

read file

In C + + programming, we use the stream extraction operator (> >) to read information from files, just as we use this operator to enter information from the keyboard. The only difference is that here you use the ifstream or fsstream object instead of the CIN object.

 

Read & write instance

The following C + + program opens a file in read-write mode. Adding file afile After the dat writes the information entered by the user, the program reads the information from the file and outputs it to the screen:

#include

#include

using namespace std;

int main ()

{

char data[100];

//Open file in write mode

ofstream outfile;

outfile.open(“afile.dat”);

cout << “Writing to the file” << endl;

cout << “Enter your name: “;

cin.getline(data, 100);

//Write user input data to the file

outfile << data << endl;

cout << “Enter your age: “;

cin >> data;

cin.ignore();

//Write the data entered by the user to the file again

outfile << data << endl;

//Close open files

outfile.close();

//Open file in read mode

ifstream infile;

infile.open(“afile.dat”);

cout << “Reading from the file” << endl;

infile >> data;

//Write data on screen

cout << data << endl;

//Read the data from the file again and display it

infile >> data;

cout << data << endl;

//Close open files

infile.close();

return 0;

}

When the above code is compiled and executed, it will produce the following inputs and outputs:

 

In the above example, additional functions of the CIN object are used, such as getline() function, which reads a line from the outside, and ignore() function will ignore the redundant characters left by the previous read statement.

 

File location pointer

Istream , and , ostream , both provide member functions for relocating file location pointers. These member functions include {seekg (“seek get”) for istream and} seekp (“seek put”) for ostream.

The parameters of seekg and seekp are usually a long integer. The second parameter can be used to specify the lookup direction. The search direction can be IOS:: beg (by default, locate from the beginning of the stream), IOS:: cur (locate from the current position of the stream), or IOS:: end (locate from the end of the stream).

The file location pointer is an integer value that specifies the number of bytes from the start of the file to the location of the pointer. The following is an example of locating the “get” file location pointer:

//Locate the nth byte of fileobject (assuming IOS:: beg)

fileObject.seekg( n );

//Move the read pointer of the file back n bytes from the current position of fileobject

fileObject.seekg( n, ios::cur );

//Move the read pointer of the file back n bytes from the end of fileobject

fileObject.seekg( n, ios::end );

//Navigate to the end of fileobject

fileObject.seekg( 0, ios::end );

Recommended Today

Springcloud – gateway

1. The version limit is based on springboot 2 X version spring 5 version       TRANSLATE with x English Arabic Hebrew Polish Bulgarian Hindi Portuguese Catalan Hmong Daw Romanian Chinese Simplified Hungarian Russian Chinese Traditional Indonesian Slovak Czech Italian Slovenian Danish Japanese Spanish Dutch Klingon Swedish English Korean Thai Estonian Latvian Turkish Finnish […]