c - 如何检查文件夹C中是否存在文件

标签 c

<分区>

我是 C 初学者,我对如何做到这一点感到有点困惑。我正在尝试使用 readdirstrcmp 函数,但它会给我带来很多错误。

#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <dirent.h>

int main(int argc, char *argv[])
{   
    DIR *dirp;
    struct dirent *direntp;

    dirp = opendir(argv[1]);
    if (dirp == NULL)
    {
        printf("File could not be open\n");

        return -1;
    }

    int i = 0;
    while((direntp[i]=readdir(dirp)) != NULL)
    {
        if(strcmp(direntp[i], argv[2]) == 0)
        {
            printf("The file %d is in directory %s my friend!", argv[2], dirp);
        }

        i++;
    }

    closedir(dirp);
    return 0;
}

最佳答案

read about access

if(access("myfile.txt", F_OK)) {
     // file exists
}

关于c - 如何检查文件夹C中是否存在文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21878770/

相关文章:

c - 使用 zlib 压缩文件

c - 如何仅分配或释放数组的一部分?

c - C的段错误

c++ - 使用 OpenCV 进行多色物体检测

c - 批处理文件不正确的目录

c - 通过函数的返回值初始化 C 结构

c - 使用递归函数以相反顺序打印数组

C 相互引用静态初始值设定项

c - av_frame_get_side_data 处的段错误

java - Java 的序列化文件与 C 语言(JNI)中的 `sizeof` 运算符不相等