fopen: 文件不存在但确实存在

fopen: file does not exist but it does

我想读取我可以在我的可执行文件所在的文件夹中找到的所有文件,但我是 运行 的可运行文件除外。我编写了以下代码,但是,尽管此列表正确地列出了我文件夹中的文件,但我无法使用 fopen 打开它们,因为 fopen 打印出该文件不存在。如果我做 gedit "path of the file obtained from my program in c" 那么它会从这个词中完美地打开。错误在哪里?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h> 
#include <unistd.h>     

int main (int argc, char **argv) {

//Determining the number of files we have.
//We call to a bash command 
FILE *fp, *fin;
char path[1035], cwd[1024];
int scanned = 0;

/* Open the command for reading. */
//https://askubuntu.com/questions/370697/how-to-count-number-of-files-in-a-directory-but-not-recursively
//This count soft and hard links also (I think)
fp = popen("ls -F |grep -v /", "r");
if (fp == NULL) {
    printf("Failed to run command\n" );
    exit(1);
}

/* Read the output a line at a time - output it. */
//Loop for each file. Be careful! if the exe is inside, it will also be counted!
while (fgets(path, sizeof(path)-1, fp) != NULL) {
    printf("Reading file: %s\n", path); 

    fin=fopen(path,"r");

    scanned = 0;
    printf("caa");

    if (fin != NULL){
        printf("AA\n");
        fclose(fin);
    }
    if (!fin)perror("fopen");
    printf("Done! \n");
}

/* close */
pclose(fp);

printf("end");

return 0;

}

您的代码中有 2 个错误:

  1. 当代码更新代码中的 "path" 变量时。它末尾有一个换行符,需要更正为 NUL。这给出了不正确的路径。 可以将类似下面的内容附加到您的代码中:

    while (fgets(path, sizeof(path)-1, fp) != NULL) {
    len=strlen(path);
    path[len-1]='[=10=]';
    
  2. 使用'ls -A1',因为'ls -F'在二进制名称中添加了一个'*':

    fp = popen("ls -A1 |grep -v /", "r");

好的,以防万一其他人需要更好的方法,我根据我的评论重新编写了代码。在这里我给你新的代码。

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

int isDirectory(const char *path) {
   struct stat statbuf;
   if (stat(path, &statbuf) != 0)
       return 0;
   return S_ISDIR(statbuf.st_mode);
}

int main (int argc, char **argv) {

    FILE *fp, *fin;
    char path[1035], cwd[1024];
    int scanned = 0;
    int ints;
    DIR *dir;
    struct dirent *ent;

    //getcwd prints directory where the app ran.
    if ((dir = opendir (getcwd(cwd, sizeof(cwd)))) != NULL) {
        /* print all the files and directories within directory */
        while ((ent = readdir (dir)) != NULL) {
            /*Skips . and ..*/
            if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) continue;
            if (isDirectory(ent->d_name) != 0)                   continue;

            printf ("Reading file: %s\n", ent->d_name);
            scanned = 0;
            fin=fopen(ent->d_name,"r");
            if (fin != NULL){
                while ((scanned = fscanf(fin, "%d", ints)) !=  EOF) {
                    if(scanned == 1){
                        printf("%d\n", ints);
                    }else {
                            printf("Whoops! Input format is incorrect!\n");
                        break;
                    }
                } //LOOP: reading file
                fclose(fin);
            }
            if (!fin)perror("fopen");
            printf("Done! \n");
        }//LOOP: while opendir
        closedir (dir);
    } else {
      /* could not open directory */
      perror ("opendir");
      return EXIT_FAILURE;
    }

  return 0;

}