current position:Home>[Qt] Provide a way to regularly backup files

[Qt] Provide a way to regularly backup files

2022-11-24 23:07:59Evenurs

1. Requirements

Regular automatic backup when opening the document to prevent data loss caused by sudden power failure.

2. Process

2.1 File structure

  1. The target file Target.
  2. Backup file Backup_X.

2.2 Open stage

  1. Judge whether the target file exists, if it does not exist, jump to the second step to create a new target file, if it exists, jump to the third step to continue.
  2. Create the target file and skip to the third step.
  3. Judge whether the backup file 1 exists, if it exists, it means that the power failure has not been saved, prompt the user whether to restore the file, and jump to the fourth step.If it does not exist, create a new backup file 1, write the target file data into backup file 1, and skip to step 5.
  4. If restoring, overwrite the content of the backup file 1 to the target file, and then write the records to the backup file 1 regularly.If not, overwrite the content in the target file to backup file 1, then write the records to backup file 1 regularly, and skip to step 5.
  5. At this time, the initial state of the target file and backup file 1 should be processed, and then enter the regular backup state.

2.3 Timing backup stage

  1. When the timing arrives, save the target file data in segments to backup file 1.

2.4 Closing phase

  1. Save the target file data before closing the target file operation.
  2. Close and delete the backup file.
  3. Close the application.

3. Function

3.1exists

Check if the file exists.

The function is used as follows:

uint32_t NewFileOpen(char* name){QFile file(name);if(file. exists()){func1();}else{func2();}}

3.2append

The filename of the concatenated backup file.

The function is used as follows:

 QString BackupFileName(name);BackupFileName.append("-backup");QFile BackupFile(BackupFileName);

3.3rename

Rename the file name.

The function is used as follows:

BackupFile.rename(BackupFileName);

3.4open

The file is opened, or created if it does not exist.

The function is used as follows:

file.open(QIODevice::ReadWrite|QIODevice::Text);

3.5close

File closed.

The function is used as follows:

file. close();

Four. Summary

The idea of ​​backing up scheduled files comes from Microsoft Word, and it is hoped that it will be packaged into a module for easy use.The source code is not yet available and is being debugged.

copyright notice
author[Evenurs],Please bring the original link to reprint, thank you.
https://en.chowdera.com/2022/328/202211242305595406.html

Random recommended