简体   繁体   English

Qt:如何锁定/防止文件在写入时被读取?

[英]Qt: How to lock/prevent a file from being read while it is written?

I am using Qt5 on Windows 7.我在 Windows 7 上使用 Qt5。
In my current project I open a binary file in order to populate it with data coming from a TCP socket.在我当前的项目中,我打开一个二进制文件,以便用来自 TCP 套接字的数据填充它。 Normally, after the file is populated, I close it and another application will read this binary file for further processing.通常,在填充文件后,我关闭它,另一个应用程序将读取此二进制文件以进行进一步处理。
Well, the problem is: The writing operation takes about 4-5 seconds (or even more) so I need to find a way to prevent the other application from reading from the binary file until the file is completely populated ...好吧,问题是:写入操作大约需要 4-5 秒(甚至更多),所以我需要找到一种方法来防止其他应用程序从二进制文件中读取,直到文件完全填充......
Here below is the code (yet I suppose it won't help much):下面是代码(但我想它不会有太大帮助):

int error = 0;
unsigned long dataLength;
char dataBuffer[1500];
QFile localFile("datafile.bin");
//
localFile.open(QIODevice::WriteOnly);
while(error == 0)
{
    error = readSocket(dataBuffer, &dataLength);
    if(error == 0)
    {
        localFile.write(dataBuffer, dataLength);
    }
    else
    {
        error = -1;
    }
}
localFile.close();

I am thinking about using a temporary file and rename it after the write operation is complete.我正在考虑使用一个临时文件并在写操作完成后重命名它。
But maybe there is another better/smarter idea?但也许还有另一个更好/更聪明的想法? Some kind of " lock file for reading " maybe...?某种“用于阅读的锁定文件”也许......?

If you have the source to both applications, then the one writing the file can signal the other application by one of many IPC mechanisms (eg local sockets) that it has finished writing.如果您拥有两个应用程序的源,那么写入文件的应用程序可以通过许多 IPC 机制(例如本地套接字)之一向另一个应用程序发出信号,表明它已完成写入。

Alternatively, write to a file with a different filename and then rename / copy the file to the location expected by the reading application, when the write has finished.或者,写入具有不同文件名的文件,然后在写入完成后将文件重命名/复制到读取应用程序预期的位置。

However, it is advisable to use QSaveFile , rather than QFile when writing out files.但是,建议在写出文件时使用QSaveFile ,而不是 QFile 。 As the documentation states: -正如文档所述: -

While writing, the contents will be written to a temporary file, and if no error happened, commit() will move it to the final file写入时,内容将写入临时文件,如果没有发生错误,commit() 会将其移动到最终文件

So this will likely solve the problem for you.因此,这可能会为您解决问题。

I know maybe it's a little bit too late, but I recently found an interesting solution, ie a component named "Locked File":我知道可能有点太晚了,但我最近发现了一个有趣的解决方案,即一个名为“锁定文件”的组件:

The QtLockedFile class extends QFile with advisory locking functions. QtLockedFile类使用咨询锁定功能扩展QFile

This class extends the QFile class with inter-process file locking capabilities.此类扩展了具有进程间文件锁定功能的 QFile 类。 If an application requires that several processes should access the same file, QtLockedFile can be used to easily ensure that only one process at a time is writing to the file, and that no process is writing to it while others are reading it.如果应用程序需要多个进程访问同一个文件,QtLockedFile 可以用来轻松确保一次只有一个进程在写入文件,并且在其他进程正在读取文件时没有进程正在写入文件。

class QtLockedFile : public QFile
{
public:
    enum LockMode { NoLock = 0, ReadLock, WriteLock };

    QtLockedFile();
    QtLockedFile(const QString &name);
    ~QtLockedFile();

    bool open(OpenMode mode);

    bool lock(LockMode mode, bool block = true);
    bool unlock();
    bool isLocked() const;
    LockMode lockMode() const;

private:
    LockMode m_lock_mode;
};

This link will lead you to the right place, where the QLockedFile class implementation is:此链接将引导您到正确的位置,其中QLockedFile类实现是:
https://github.com/kbinani/qt-solutions/tree/master/qtlockedfile https://github.com/kbinani/qt-solutions/tree/master/qtlockedfile

** So, I decided to share this info, maybe other Qt-users are interested! ** 所以,我决定分享这个信息,也许其他 Qt 用户会感兴趣! ** **

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM