从不兼容的指针类型和取消引用指针到不完整类型 <dirent.h> 的赋值



我正在尝试编写一个程序来读取有关程序所在文件夹的信息。我已经仔细检查了其他在线资源,看起来我使用的是与其他正确解决方案相同的模式(见下文(。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
void append(char* path, char* cur)
{
    strcat("/",cur);
    strcat(cur,path);
}
void addDot(char* dot)
{
    int len = strlen(dot);
    dot[len] = '.';
}
int main()
{
    struct stat st;
    int stDevCur = st.st_ino;
    int stDevNew = 0;
    int inodeCur = st.st_dev;
    int inodeNew = 0;
    char* path = "";
    char* new = "";
    char* cur = "_";
    char* dot = ".";
    DIR* dir;
    struct dirnet* file = NULL;
    if((dir = opendir(dot)) == NULL)
    {
        perror(NULL);
        exit(EXIT_FAILURE);
    }
    while(strcmp(cur,new) != 0)
    {
        if((file = readdir(dir)) == NULL)
        {
            perror(NULL);
            exit(EXIT_FAILURE);
        }
        while(file != NULL)
        {
            inodeNew = st.st_ino;
            stDevNew = st.st_dev;
            if(inodeNew == inodeCur && stDevNew == stDevCur)
            {
               cur = file->d_name;
               append(path, cur);
               break;
             }
        }
        fprintf(stderr,"%sn",path);
    }
    return 0;
}

给出以下错误

main.c: In function ‘main’:
main.c:42:16: error: assignment from incompatible pointer type [-Werror]
       if((file = readdir(dir)) == NULL)
                ^
main.c:53:23: error: dereferencing pointer to incomplete type
         cur = file->d_name;
                   ^
cc1: all warnings being treated as errors
make: *** [main.o] Error 1

自昨晚以来,我一直在研究这个问题,并一直在将它们与堆栈溢出上的类似问题进行比较

struct dirnet* file = NULL;

问题是拼写错误。它应该是:

struct dirent* file = NULL;

最新更新