c - 检查目录时出现指针问题

标签 c pointers gcc

我正在制作一个函数(一个更大项目的一部分),它检查 C 中是否存在文件。这是代码:

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
int file_exist(file_path, file_name)
{
  DIR *dp;
  FILE *fc;
  struct dirent *ep;

 dp = opendir(file_path);
 fc = fopen(file_name, "r");
  if(dp = NULL)
    error_escape("Opening path");
  else
    chdir(file_path);
  if(fc = NULL)
  {
    error_escape("Opening file");
    return(-1);
  }
  else
    return(0);
}

但是,当我尝试编译它时,我得到了这个结果:

file_exist.c: In function ‘file_exist’:
file_exist.c:10:15: warning: passing argument 1 of ‘opendir’ makes pointer from integer without a cast
  dp = opendir(file_path);
               ^
In file included from file_exist.c:3:0:
/usr/include/dirent.h:134:13: note: expected ‘const char *’ but argument is of type ‘int’
 extern DIR *opendir (const char *__name) __nonnull ((1));
             ^
file_exist.c:11:13: warning: passing argument 1 of ‘fopen’ makes pointer from integer without a cast
  fc = fopen(file_name, "r");
             ^
In file included from file_exist.c:1:0:
/usr/include/stdio.h:272:14: note: expected ‘const char * __restrict__’ but argument is of type ‘int’
 extern FILE *fopen (const char *__restrict __filename,
              ^

我已经在很多地方寻找解决方案,并尝试了许多不同的方法来查看是否可以修复它,但我就是不知道该怎么做。预先感谢您提供的所有帮助。

最佳答案

file_exist.c: In function ‘file_exist’:
file_exist.c:10:15: warning: passing argument 1 of ‘opendir’ makes pointer from integer without a cast
  dp = opendir(file_path);
               ^

这个错误是因为你的函数声明为

int file_exist(file_path, file_name)

由于未声明参数的类型,因此假定它们是 int。您必须将这一行替换为

int file_exist(char *file_path, char *file_name)

但是,正如 David van rijn 指出的那样,问题在于您在做什么

if(dp = NULL)

并将 dp 的值替换为 NULL 而不是检查它。你需要写

if(dp == NULL)

此外,您的代码中存在逻辑错误(在这些修复之后):

int file_exist(char *file_path, char *file_name)
{
  DIR *dp;
  FILE *fc;
  struct dirent *ep;

  dp = opendir(file_path);
  fc = fopen(file_name, "r");
  if(dp == NULL)
    error_escape("Opening path");
  else
    chdir(file_path);
  if(fc == NULL)
  {
    error_escape("Opening file");
    return(-1);
  }
  else
    return(0);
}

因为您在转到正确的目录之前打开文件。您需要在 fopen 之前执行 chdir:

int file_exist(char *file_path, char *file_name)
{
  DIR *dp;
  FILE *fc;
  struct dirent *ep;

  dp = opendir(file_path);
  if(dp == NULL)
    error_escape("Opening path");
  else
    chdir(file_path);

  fc = fopen(file_name, "r");
  if(fc == NULL)
  {
    error_escape("Opening file");
    return(-1);
  }
  else
    return(0);
}

最后,您的代码正在泄漏文件描述符,因为您没有关闭正在打开的目录和文件。你需要做类似的事情

int file_exist(char *file_path, char *file_name)
{
  DIR *dp;
  FILE *fc;
  struct dirent *ep;

  dp = opendir(file_path);
  if(dp == NULL)
    error_escape("Opening path");
  else
    chdir(file_path);
  closedir(dp);

  fc = fopen(file_name, "r");
  if(fc == NULL)
  {
    error_escape("Opening file");
    return(-1);
  }
  else {
    close(fc);
    return(0);
  }
}

一个稍微好一点的方法是:

int file_exist(char *file_path, char *file_name)
{
  int dir;
  FILE *fc;

  dir = chdir(file_path);
  if (dir == -1) {
    error_escape("Opening path");
    return -1;
  }

  fc = fopen(file_name, "r");
  if(fc == NULL)
  {
    error_escape("Opening file");
    return(-1);
  }
  else {
    close(fc);
    return(0);
  }
}

关于c - 检查目录时出现指针问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35398850/

相关文章:

C:双向链表中的段错误,在追加和删除函数中

c - 原因 main-C 不是 const

c++ - GCC中enum switch的控制流不足分析

android - 在 Dl_Info 上编译失败

c - libav - avcodec_video_encode2() + fwrite() = 非功能文件

c++ - C/C++ 中单个参数(函数)中的多个参数

c - 如何在C中获取子字符串

c++ - 使用指向映射的指针从其键访问映射的值

c++ - 删除 C++ 中的每个指针作为指向数组的指针是否安全?

c - 出现错误 : expected declaration specifiers or '...' before string constant