Read directory from console in C

7

Hello, I have the following code:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <dirent.h> // Libreria encargada del tema de los directorios
#include <string.h>

void load(){
    char dir[250];
    DIR *carpeta;
    struct dirent *archivo;

    CLEAR
    printf("Arrastre la carpeta a la ventana o ingrese la direccion para indexar\n");
    fgets(dir,250,stdin);

    carpeta = opendir(dir);

    if (carpeta != NULL){
        while ((archivo = readdir(carpeta))) puts (archivo->d_name);
        closedir(carpeta);
    }else
        perror ("Error al abrir el directorio ");

    getchar();
}

Given an address of a folder, it lists the files that are inside it, the problem is that it simply does not read the files when I drag them to the console, but it does when I write the address directly in the file .c

Here drag the folder to the window:

Here, make a change to the code and directly enter the same address as the folder you drag

carpeta = opendir("/Users/cristoferfb/Documents/Universidad");

As you can see on the second occasion, he carries out his task without any problem.

It should be noted that when entering folders with spaces in the names strange symbols are generated:

Those \ are not part of the name and I tried to remove them but in the end it does not work either.

pd: I'm working under macOs Sierra with gcc

    
asked by Cristofer Fuentes 08.05.2017 в 17:37
source

1 answer

2

For the first example based on your images - > "/Users/cristoferfb/Documents/Universidad" I did a similar test and it does not work because fgets() reads / captures the newline for example try the following:

if(isspace(dir[strlen(dir)-1]))
          dir[strlen(dir)-1]='
    char dir[250];
    DIR *carpeta;
    struct dirent *archivo;

    //CLEAR
    printf("Arrastre la carpeta a la ventana o ingrese la direccion para indexar\n");
    fgets(dir,250,stdin);

    if(isspace(dir[strlen(dir)-1]))
                dir[strlen(dir)-1]='
    char dir[250];
    DIR *carpeta;
    struct dirent *archivo;

    //CLEAR
    printf("Arrastre la carpeta a la ventana o ingrese la direccion para indexar\n");

    fgets(dir,250,stdin);

    if(isspace(dir[strlen(dir)-1]))
                dir[strlen(dir)-1]='
if(isspace(dir[strlen(dir)-1]))
          dir[strlen(dir)-1]='
    char dir[250];
    DIR *carpeta;
    struct dirent *archivo;

    //CLEAR
    printf("Arrastre la carpeta a la ventana o ingrese la direccion para indexar\n");
    fgets(dir,250,stdin);

    if(isspace(dir[strlen(dir)-1]))
                dir[strlen(dir)-1]='
    char dir[250];
    DIR *carpeta;
    struct dirent *archivo;

    //CLEAR
    printf("Arrastre la carpeta a la ventana o ingrese la direccion para indexar\n");

    fgets(dir,250,stdin);

    if(isspace(dir[strlen(dir)-1]))
                dir[strlen(dir)-1]='%pre%';

    int j = 0;

     while (dir[j] != '%pre%'){

        if (dir[j] == '\' ) {

           int idx = j; 

           memmove(&dir[idx], &dir[idx + 1], strlen(dir) - idx);

        }
        j++;
    }

    carpeta = opendir(dir);

    if (carpeta != NULL){
        while ((archivo = readdir(carpeta))) puts (archivo->d_name);
        closedir(carpeta);
    }else
        perror ("Error al abrir el directorio ");

    getchar();
'; carpeta = opendir(dir); if (carpeta != NULL){ while ((archivo = readdir(carpeta))) puts (archivo->d_name); closedir(carpeta); }else perror ("Error al abrir el directorio "); getchar();
';
'; int j = 0; while (dir[j] != '%pre%'){ if (dir[j] == '\' ) { int idx = j; memmove(&dir[idx], &dir[idx + 1], strlen(dir) - idx); } j++; } carpeta = opendir(dir); if (carpeta != NULL){ while ((archivo = readdir(carpeta))) puts (archivo->d_name); closedir(carpeta); }else perror ("Error al abrir el directorio "); getchar();
'; carpeta = opendir(dir); if (carpeta != NULL){ while ((archivo = readdir(carpeta))) puts (archivo->d_name); closedir(carpeta); }else perror ("Error al abrir el directorio "); getchar();
';

.

%pre%

I can not try to drop the directories in the terminal because the one I use does not accept that, but the system it uses is very similar and works for such a route.

/ruta/a/sin/espacios_ni_descendente

If it does not work for that style of routes, you have to clean the \ before passing them to opendir see if content of dir after using if(isspace... have those characters and replace them. but as I say this part I can not look at it, even the first error is because of what is said about fgets .

With \ I mean /Users/cristoferfb/Documents/Universidad/Estructura\ de\ datos

  

UPDATE:

I've done some tests and with a path like this /Users/cristoferfb/Documents/Universidad/Estructura de datos or like this other /Users/cristoferfb/Documents/Universidad/Estructura de datos. should work, what I mean is that when you set dir and after apliar the if(isspace... apply a function remove the \ backslash, but leave the next space as shown above. This I think should be enough but if not, try to add a . at the end

I'll give you an example (take it as a pseudocode) because it is made to illustrate and although it works it can be improved.

%pre%     
answered by 08.05.2017 / 20:31
source