c + + reading binary bin file

Posted by marcela1637 on Sun, 20 Feb 2022 19:14:51 +0100

ofstream is from memory to hard disk, and ifstream is from hard disk to memory. In fact, the so-called stream buffer is memory space
In C + +, there is a stream class. All I/O is based on this "stream" class, including the file I/O we need to know
1. Inserter (< <)

Output data to the stream. For example, the system has a default standard output stream (cout), which generally refers to the display. Therefore, cout < < Write Stdout < < '\ n'; This means that the string "Write Stdout" and newline character ('\ n') are output to the standard output stream.

2. Extractor (> >)

Enter data from the stream. For example, the system has a default standard input stream (cin), which generally refers to the keyboard. Therefore, CIN > > x; It means reading a specified type of data from the standard input stream.
In C + +, the operation of files is realized through the subclass fsstream (file stream) of stream. Therefore, to operate files in this way, you must add the header file fsstream h.

fstream also has the same constructor as open(). For the above example, the file can be opened when defined:
  fstream file1("c:\config.sys");

In particular, fstream has two subclasses:

ifstream(input file stream) and OFSTREAM (output file stream),
ifstream opens the file as input by default
ofstream opens the file as output by default.

II. Closing documents
The open file must be closed after use. fstream provides a member function close() to complete this operation,
For example: file1 close();
Close the file connected to file1.

To read and write binary data blocks, use the member functions read() and write(). Their prototypes are as follows:
 read(unsigned char *buf,int num);
 write(const unsigned char *buf,int num);
read() reads num characters from the file to the cache pointed to by buf. If it reaches the end of the file before reading num characters, you can use the member function int gcount(); To obtain the actual number of characters read; write() writes num characters from the cache pointed to by buf to the file. It is worth noting that the type of cache is unsigned char *, and sometimes type conversion may be required.

V. document positioning
Different from the file operation mode of C, the C++ I/O system manages two pointers associated with a file. One is the read pointer, which indicates the position of the input operation in the file; The other is the write pointer, which is the position of the next write operation. Each time the input or output is executed, the corresponding pointer changes automatically. Therefore, C + + file location is divided into read location and write location, and the corresponding member functions are seekg() and seekp(). Seekg() is to set the read position and seekp is to set the write position. Their most common forms are as follows:
  istream &seekg(streamoff offset,seek_dir origin);
  ostream &seekp(streamoff offset,seek_dir origin);
streamoff is defined in iostream In H, the maximum value that can be obtained with offset is defined, seek_dir indicates the base position of the movement. It is an enumeration with the following values:
ios::beg: beginning of file
ios::cur: current location of the file
ios::end: end of file
These two functions are generally used for binary files, because the text file may differ from the expected value due to the system's interpretation of characters. Example:
  file1.seekg(1234,ios::cur); // Move the read pointer of the file back 1234 bytes from the current position
  file2.seekp(1234,ios::beg); // Move the write pointer of the file back 1234 bytes from the beginning of the file

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

//int  readBinFile(std::string& filename, void*& bufPtr, int& pointNum, int pointDim)

int main()
    // open the file:
    std::streampos fileSize;  //Instantiated fpos is used to represent the position in narrow flow.
    std::ifstream file("/home/oem/CLionProjects/untitled/a.bin", std::ios::binary);

    void *bufPtr=0;
    int pointNum=30;

    int pointDim=5;

    if (!file) {
        return false;
    // get its size:
    file.seekg(0, std::ios::end);
    fileSize = file.tellg();

    file.seekg(0, std::ios::beg);
    bufPtr = malloc(fileSize);
    if(bufPtr == nullptr){
        return false;

    // read the data:

    file.read((char*) bufPtr, fileSize);

    int i=0;
    file.read((char *) &bufPtr, sizeof 10);

    for(i = 0; i <5; i++) // show values read from file
        cout << bufPtr[i] << " ";

    pointNum = fileSize /sizeof(float) / pointDim;

    if( fileSize /sizeof(float) % pointDim != 0);

    return 1;
    int main()
    {        int pointNum = 0;
        void *inputPointBuf = nullptr;

        std::string s0("a.bin");}


Topics: C++ Back-end