使用std:fstream如何拒绝对文件的访问(读和写) [英] Using std:fstream how to deny access (read and write) to the file

查看:284
本文介绍了使用std:fstream如何拒绝对文件的访问(读和写)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何拒绝访问使用fstream打开的文件?

How can I deny access to a file I open with fstream? I want to unable access to the file while I'm reading/writing to it with fstream?

推荐答案

我不能使用fstream来读取/写入文件。

You cannot do it with the standard fstream, you'll have to use platform specific functions.

在Windows上,您可以使用 CreateFile() LockFileEx()。在Linux上,有 flock() lockf() fcntl()(作为上一个评论者

On Windows, you can use CreateFile() or LockFileEx(). On Linux, there is flock(), lockf(), and fcntl() (as the previous commenter said).

如果使用MSVC,可以将第三个参数传递给fstream的构造函数。请参阅 Visual Studio 6 较新版本。当然,它不能与其他编译器和平台一起使用。

If you are using MSVC, you can pass a third parameter to fstream's constructor. See the documentation for Visual Studio 6 or newer versions. Of course it won't work with other compilers and platforms.

为什么要锁定其他人?可能有更好的解决方案...

Why do you want to lock others out anyway? There might be a better solution...

这篇关于使用std:fstream如何拒绝对文件的访问(读和写)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆