Get the App
SLTechnology News&Howtos  ›  Development  › 

Introduction to the method and function of random reading and writing of files in C++

Shulou Source: shulou.com Published: 2022-06-03 19:37:24 09月23日 Update

This article mainly introduces "the method and function of random reading and writing of files in C++". In daily operation, I believe many people have doubts about the method and function of random reading and writing of documents in C++. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "the introduction of random reading and writing methods and their functions of documents in C++". Next, please follow the editor to study!

Catalogue

Overview

Random read and write

Function

Examples

Pointer stream member function

Random access to binary data

Overview

The operation of files can be divided into sequential reading and writing and random reading and writing. Sequential read and write means that the pointer to a file can only be moved from head to tail. Random read and write means that the file pointer can be moved at will as needed.

Random read and write

File pointer: in a disk file operation, there is a file pointer that indicates where to read and write.

Function

The file stream provides some member functions about file pointers:

The member function functions gcount () to return the number of bytes read in the last input tellg () returns the current position of the input file pointer seekg (location in the file) moves the pointer in the input file to the specified location seekg (displacement) Tellp () returns the current position of the output file pointer seekp (location in the file) moves the pointer in the output file to the specified location seekp (displacement, reference position) moves several bytes based on the reference position

G-for input functions (get), such as seekg for output files

P-for output functions (put), such as seekp for output files

Other-can both input and output files, any messenger two sets of functions.

Examples

Enter 10 integers from the keyboard and save them to the data file F1. In dat, read the data from the file and display it on the screen.

# include # include using namespace std;int main () {int a [10], b [10]; / / Open the file fstream iofile ("temp.txt", ios::in | ios::out); if (! iofile) {cerr

Tags: File location pointer data function move output input function method student member clocked three character learning binary byte example basic Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Linux Xiaomi Docker NVidia