S_ISREG returns 0

S_ISREG returns 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 file\n");
        return -1;
    }
    close(fd);
    return 0;
}

我运行:.\a in.txt

我不知道到底发生了什么,但是当我试图测试文件是否正常时(最后一个 if 语句),它失败了。我测试了 fstat 是否失败,但没有。

这是问题所在:

if ((fd = open(argv[1], O_RDONLY) == -1)) {

相等运算符 == 的优先级高于赋值运算符 =。所以上面的解析为:

if (fd = (open(argv[1], O_RDONLY) == -1)) {

将比较结果分配给 fd,比较结果为 0 或 1。这些值恰好都是 stdin 和 stdout 的有效打开文件描述符,因此 fstat 调用成功并让您了解这些流之一的状态。

您需要先调整括号才能完成作业:

if ((fd = open(argv[1], O_RDONLY)) == -1) {

此外,您似乎还有其他 if 语句,其中有一组多余的括号,您可以将其删除。你想避免这种情况,因为那些额外的括号可以消除关于你所做的事情的警告。