Read 1KB of each file in a folder

0

I need to read the first KB of each file inside a folder, I can show the names of all the files on the screen but then I can not figure out what to do to read each file one by one

#include <stdio.h>
#include <stdlib.h>
#include "antispam.h"
#include <sys/types.h>
#include <dirent.h>
int main(int argc, char *argv[])
{
/* Variables */
DIR *dirp;
struct dirent *direntp;

if(argc != 2){
    printf("Argumentos invalidos\n");
    exit(1);
}else{
    /* Abrimos el directorio */
    dirp = opendir(argv[1]);
    if (dirp == NULL){
    printf("Error: No se puede abrir el directorio\n");
    exit(2);
    }

    /* Leemos las entradas del directorio */
    while ((direntp = readdir(dirp)) != NULL) {
        /*  FILE *f = fopen(direntp->d_name, "r");*/

          printf("%s\n",direntp->d_name);
    }
    /*
printf("numero de argumentos %d\n",(int)argc);
printf("argumento 1 %s\n",argv[0]);
printf("argumento 2 %s\n",argv[1]);
*/

    /* Cerramos el directorio */
    closedir(dirp);

}

return 0;
}
    
asked by Christian 07.10.2018 в 15:36
source

1 answer

0

You can do it by replacing the line:

printf("%s\n",direntp->d_name);

With something like the following:

char path[1024];
char firstKB[1024];
sprintf(path, "%s/%s", argv[1], direntp->d_name);
FILE *f = fopen(path, "r");
fread(firstKB, 1024, 1, f);
// haz algo con el primer KB
    
answered by 12.10.2018 в 22:28