当我将chemin中的open(chemin, O_RDONLY)硬编码到一个文件名时,程序工作,但当我离开时,如果open(chemin, O_RDONLY)我得到No such file or directory
为什么不在类型ficher中使用chemin
当我在printf("%s", chemin)中使用type_fichier时,我得到'

int type_fichier(char * chemin) {
  int fp;
  if ((fp = open(chemin, O_RDONLY)) == -1) { perror(""); exit(0); }

  struct stat fileStat;
  if(fstat(fp, &fileStat) < 0)
    return 1;

  switch(fileStat.st_mode & S_IFMT) {
    case S_IFBLK:  printf("block device\n");            break;
    case S_IFCHR:  printf("character device\n");        break;
    case S_IFDIR:  printf("directory\n");               break;
    case S_IFIFO:  printf("FIFO/pipe\n");               break;
    case S_IFLNK:  printf("symlink\n");                 break;
    case S_IFREG:  printf("regular file\n");            break;
    case S_IFSOCK: printf("socket\n");                  break;
    default:       printf("unknown?\n");                break;
  }
}

int main(int argc, char *argv[]) {
  char fn = "file";
  type_fichier(&fn);
}

最佳答案

您的错误在于,您只为fn分配了一个字符:

char fn = "file";
type_fichier(&fn);

你可能想要:
const char *fn = "file";
type_fichier(fn);

关于c - C:没有这样的文件或目录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16926726/

10-15 06:12