QFile related operations

1. Conversion between QString, QByteArray, char *

(1) QString -> QByteArray

 QString str = "123";
 QByteArray byteArray = str.toUtf8(); // 中文
 byteArray = str.toLocal8Bit(); // local encoding

(2) QByteArray -> char *

 char *pc = byteArray.data();

(3) char * -> QString

 char *pc = "abc";
 QString qstr = QString("%1").arg(pc);

2. Reading and writing small files

2.1 Read all the data in the file

     QString fileName = QString("%1").arg("D:/01.txt");
     QFile file(fileName); // path of fileName file
     if(file.open(QIODevice::ReadOnly)) // open the file for reading and writing
     {
         return ;    
     }
     QByteArray data = file.readAll(); // Method 1. Read all the data in the file
   
     int fileSize = file.size(); // Get the length of the file (unit: byte)
     data = file.read(fileSize); // Method 2. Read all the data of the file
     // Start reading from the specified position
     file.seek(100) // This is set to read from position 100
     data = file.read(1000) // read 1000 bytes from position 100
     file.close(); // Close the file after use

2.2 Read file data line by line

     QString fileName = QString("%1").arg("D:/01.txt");
     QFile file(fileName); // path of fileName file
     if(file.open(QIODevice::ReadOnly)) // open the file for reading
     {
         return ;
     }
     while(!file.atEnd()) // Determine whether the file is over
     {
         // Every time a row of data is read, the cursor seek will automatically jump down, so when the seek reaches the end, atEnd() returns true
         QByteArray data =  file.readLine();  
     }
     file.close();

2.3 Write data to file

     QString fileName = QString("%1").arg("D:/01.txt");
     QFile file(fileName); // path of fileName file
     if(file.open(QIODevice::WriteOnly)) // open the file for writing
     {
         return ;
     }
    QByteArray data;
    data.resize(10);
    memset(data.data(),0x00,data.size());
    file.write(data);
    file.close();

2.4 Reading and writing files through memory mapping

 uchar *QFileDevice::map(qint64 offset, qint64 size, MemoryMapFlags flags = NoOptions)

        ① Map the file size bytes into memory from offset. For the mapping to succeed, a file should be open, but the file does not need to remain open after the memory is mapped. Any mappings that have not been unmapped will be automatically unmapped when the QFile is destroyed or a new file is opened using this object.
         ② The map will have the same open mode (read and/or write) as the file, unless the MapPrivateOption is used, in which case it is always possible to write to the mapped memory.
        ③ Return a pointer to memory, or return 0 if there is an error.

     QString fileName = QString("%1").arg("D:/01.txt");
     QFile file(fileName); // path of fileName file
     if(file.open(QIODevice::WriteOnly)) // open the file for writing
     {
         return ;
     }
     uchar* pfile = file.map(0, file.size()); // returns a pointer to memory, or 0 if there is an error
     if(pfile)
     {
         // do work 
         // memcpy(pfile,data.data(),size); // write data
         // memcpy(data.data(),pfile,size); // read data
         file.unmaps(pfile); // To unmap, the QFile object will be automatically unmapped if it is destroyed or a new file is opened
     }
     file.close();

3. Reading and writing large files

3.1 Split all large files into pieces for reading and writing.

     QString fileName = QString("%1").arg("D:/01.txt");
     QFile file(fileName); // path of fileName file
     if(file.open(QIODevice::WriteOnly)) // open the file for writing
     {
         return ;
     }
     qint64 fileSize = file.size(); // length of the file
     int readSize = 1024*1024 ; // The length of data read each time can be set freely
     // file.pos() gets the position of the file cursor
     while(!file.atEnd())  
     {
         QByteArray data = file.read(readSize);
     } 
     file.close();
 ​

3.2. Split all large files into read and write line by line.

     QString fileName = QString("%1").arg("D:/01.txt");
     QFile file(fileName); // path of fileName file
     if(file.open(QIODevice::WriteOnly)) // open the file for writing
     {
         return ;
     }
     while(!file.atEnd())  
     {
         QByteArray data = file.readLine();
     }
     file.close();
 ​

Guess you like

Origin blog.csdn.net/liangfei868/article/details/125969790