如何获取C中打开的fd的标志?

如何获取C中打开的fd的标志?,c,linux,fcntl,C,Linux,Fcntl,我想得到fd的标志在C中打开之前 但是我使用fcntl手册页引用的fcntl(fd,F_GETFD,0)reference,它总是向我返回1 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #define XSZ(x) (int)(sizeof(x)*2) int main() {

我想得到fd的标志在C中打开之前

但是我使用fcntl手册页引用的
fcntl(fd,F_GETFD,0)
reference,它总是向我返回1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

#define XSZ(x) (int)(sizeof(x)*2)

int main()
{
    int ret;
    static const char str [] = "hello c program!\n";
    int fd = open("test.txt", O_RDWR | O_APPEND | O_CREAT, 0777);
    if(fd < 0)
    {
        perror("open");
        return -1;
    }
    printf("fd = %d\n", fd);

    ret = fcntl(fd, F_GETFD, 0);
    printf("flag:%d\n",ret);
    write(fd, str, strlen(str)-1);

    return 0;
}

我认为ret是
O|RDWR | O|u APPEND | O|u create
F_GETFD
的总和,它不查询打开的标志,只查询
FD|u CLOEXEC
(请参阅)

线路

write(fd, "hello c program\n", strlen("hello c program!\n"));
是错误的,因为您查询的字符串长度比您写入的字符串长度长,可能导致缓冲区溢出。一种更安全、更有效的方法是:

static const char str [] = "hello c program!\n";
write(fd, str, sizeof(str)-1);
需要
-1
,以避免写入终止的0字节

我不知道这个计划的目的

#define XSZ(x) (int)(sizeof(x)*2)

但是将
size\u t
(结果类型为
sizeof()
)转换为
int
可能不是一个好主意。

F\u GETFD
不查询打开标志,只查询
FD\u CLOEXEC
(请参阅)

线路

write(fd, "hello c program\n", strlen("hello c program!\n"));
是错误的,因为您查询的字符串长度比您写入的字符串长度长,可能导致缓冲区溢出。一种更安全、更有效的方法是:

static const char str [] = "hello c program!\n";
write(fd, str, sizeof(str)-1);
需要
-1
,以避免写入终止的0字节

我不知道这个计划的目的

#define XSZ(x) (int)(sizeof(x)*2)

但是将
size\u t
(结果类型为
sizeof()
)转换为
int
可能不是一个好主意。

您应该使用F_GETFL而不是F_GETFD。还记得用八进制打印以进行比较

重要的一点是,并非所有标志都由fcntl返回。只会记住访问模式标志,如O_RDWR。但是,O_CREATE/O_TRUNC等是操作模式或打开时间标志,系统不会记住这些标志,因此不会返回

这是你修改过的代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

#define XSZ(x) (int)(sizeof(x)*2)

int main()
{
    int ret;
    int fd = open("test.txt", O_RDWR | O_APPEND | O_CREAT, 0777);
    if(fd < 0)
    {
        perror("open");
        return -1;
    }
    printf("fd = %d\n", fd);
    ret = fcntl(fd, F_GETFL);
    perror("fcntl");
    printf("flag:%o\n",ret);
    printf("flag:%o\n",O_RDWR|O_APPEND);
    write(fd, "hello c program\n", strlen("hello c program!\n"));

    return 0;
}

您应该使用F_GETFL而不是F_GETFD。还记得用八进制打印以进行比较

重要的一点是,并非所有标志都由fcntl返回。只会记住访问模式标志,如O_RDWR。但是,O_CREATE/O_TRUNC等是操作模式或打开时间标志,系统不会记住这些标志,因此不会返回

这是你修改过的代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

#define XSZ(x) (int)(sizeof(x)*2)

int main()
{
    int ret;
    int fd = open("test.txt", O_RDWR | O_APPEND | O_CREAT, 0777);
    if(fd < 0)
    {
        perror("open");
        return -1;
    }
    printf("fd = %d\n", fd);
    ret = fcntl(fd, F_GETFL);
    perror("fcntl");
    printf("flag:%o\n",ret);
    printf("flag:%o\n",O_RDWR|O_APPEND);
    write(fd, "hello c program\n", strlen("hello c program!\n"));

    return 0;
}

从你的历史来看,你似乎不接受别人给你的答案。你对他们有那么不满意吗?对SO社区来说,这不是一个很大的激励因素。@xhienne谢谢你的建议,我接受几周前提出的答案。从你的历史来看,你似乎不接受别人给你的答案。你对他们有那么不满意吗?对于SO社区来说,这不是一个很大的激励因素。@xhienne谢谢你的建议,我接受了几个星期前提出的答案。还有什么可以得到开放fd的标志吗?很抱歉出现了一些伪代码。确实存在。是否仍然可以获取打开的fd的标志?对不起,有一些伪代码。标志:102002,第一个1表示什么?这是一个大文件。默认情况下可以设置。标志:102002,第一个1表示什么?这是一个大文件。默认情况下,它可能会被设置。