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 inside it, the problem is that it just doesn't read the files when I drag them to the console, but it does when I type the address directly into the file.c
Here drag the folder to the window:
Here I made a change in the code and directly entered the same address of the folder that I dragged
carpeta = opendir("/Users/cristoferfb/Documents/Universidad");
As you can see on the second occasion it performs its 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 have tried to remove them but in the end it does not work either.
ps: I am working under macOs Sierra with gcc
For the first example based on your images ->
"/Users/cristoferfb/Documents/Universidad"
I did a similar test and it doesn't work because itfgets()
reads/captures thenewline
example try the following:.
I can't test dropping the directories in the terminal because the one I use doesn't accept that, but the system it uses is very similar and it worked for a path like this.
/ruta/a/sin/espacios_ni_descendente
If it doesn't work for that style of route, you still have to clean them
\
before passing them byopendir
checking if the contentdir
after the use ofif(isspace...
has those characters and replace them. but as I say this part I cannot look at it, even the first error is because of what is said about thefgets
.by
\
i mean/Users/cristoferfb/Documents/Universidad/Estructura\ de\ datos
I have done some tests and with a route like this
/Users/cristoferfb/Documents/Universidad/Estructura de datos
or like this one it/Users/cristoferfb/Documents/Universidad/Estructura de datos.
should work, what I mean is that when assigning todir
and after applying theif(isspace...
apply a function that eliminates the \ backslash, but leaves the following space as shown higher. I think this should be enough but if not, try adding a.
at the endI leave you an example (take it as pseudocode) because it is made to illustrate and although it works it can be improved.