gpt4 book ai didi

c - 独家开卷

转载 作者:行者123 更新时间:2023-11-30 17:01:00 26 4
gpt4 key购买 nike

通过将 dwShareMode 设置为 0,我可以使用 CreateFile “独占”打开卷:

#include <windows.h>
int main() {
HANDLE ki = CreateFile("\\\\.\\F:", GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
}

我可以使用 fopen 以“共享模式”打开卷:

#include <stdio.h>
int main() {
FILE* ki = fopen("\\\\.\\F:", "r+b");
}

我可以用 open 来“独占”打开一个文件:

#include <stdio.h>
#include <fcntl.h>
int main() {
int ju = open("lima.txt", O_RDWR | O_EXCL);
FILE* ki = fdopen(ju, "r+b");
}

但是,如果我尝试使用 open 打开卷,它将失败:

#include <stdio.h>
#include <fcntl.h>
int main() {
int ju = open("\\\\.\\F:", O_RDWR | O_EXCL);
FILE* ki = fdopen(ju, "r+b");
}

经过测试,无论有或没有 O_EXCL 标志都会发生这种情况。是独家卷打开一些只能用 CreateFile 完成的东西,或者我错过了东西?

最佳答案

根据the standard :

result is undefined if O_RDWR is applied to a FIFO

在这种情况下,卷似乎被识别为 FIFO。修复:

open("\\\\.\\F:", O_RDONLY);

或者:

open("\\\\.\\F:", O_WRONLY);

或者:

open("\\\\.\\F:", O_RDONLY | O_WRONLY);

关于c - 独家开卷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37265683/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com