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

时间:2017-05-19 10:49:17

标签: c file unix fopen

我想读取我可以在我的可执行文件所在的文件夹中找到的所有文件,除了我正在运行的runnable文件。我编写以下代码但是,虽然这列出了我文件夹中的文件,但是我无法用fopen打开它们,因为fopen打印出文件不存在。如果我做gedit“从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 http://stackoverflow.com/questions/646241/c-run-a-system-command-and-get-output
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 个答案:

答案 0 :(得分:3)

您的代码中有2个错误:

  1. 当代码更新&#34;路径&#34;代码中的变量。它最后有一个换行符,需要更正为NUL。这给出了错误的路径。 下面的内容可以附加到您的代码中:

    while (fgets(path, sizeof(path)-1, fp) != NULL) {
    len=strlen(path);
    path[len-1]='\0';
    
  2. 使用&#39; ls -A1&#39;,因为&#39; ls -F&#39;添加&#39; *&#39;以二进制名称:

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

答案 1 :(得分:3)

好吧,以防其他人需要更好的方法,我用我的评论重新编写代码。在这里,我告诉你新的代码。

#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;

}