小编MrA*_*eiL的帖子

S_ISREG 返回 0

所以我想测试给定的文件是否正常。

#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h> 
#include <unistd.h>
#include <string.h>
#include <errno.h>

int main(int argc, char **argv)
{
    // Input check.
    if (argc != 2) {
        fprintf(stdout,"Format: %s <filename.txt>\n", argv[0]);
        return -1;
    }
    // Make sure the file is a regular file.
    int fd;
    if ((fd = open(argv[1], O_RDONLY) == -1)) {
        fprintf(stdout, "%s", strerror(errno));
        return -1;
    }
    struct stat st;
    if ((fstat(fd, &st) == -1)) {
        fprintf(stdout, "%s\n", strerror(errno));
        return -1;
    }
    if (!(S_ISREG(st.st_mode))) {
        fprintf(stdout, "Error, invalid …
Run Code Online (Sandbox Code Playgroud)

c file-io posix

2
推荐指数
1
解决办法
40
查看次数

标签 统计

c ×1

file-io ×1

posix ×1