Search code examples
cfileunixfopen

fopen: file does not exist but it does


I want to read all files that I can find in the folder where my executable is, except the runnable file that I'm running. I code the following code but, although this list correctly the files that I have in my folder, I cannot open them with fopen because fopen prints that the file doesn't exists. If I do gedit "path of the file obtained from my program in c" then it opens perfectly from the term. Where is the bug?

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

}

Solution

  • There are 2 bugs in your code:

    1. when the code updates the "path" variable in your code. It has a newline at the end which needs to be corrected to NUL. This gives an incorrect path. Something like below can be appended to your code:

      while (fgets(path, sizeof(path)-1, fp) != NULL) {
      len=strlen(path);
      path[len-1]='\0';
      
    2. Use 'ls -A1', since 'ls -F' adds a '*' in binary name:

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