#include<dirent.h>
#include<stdio.h>
#include<sys/stat.h>
const char *actual_dir = "D:/Programacao/Aprendizagem/C - Completo e Total/cap9/";
int main()
{
DIR *dr;
struct dirent *datum;
struct stat buf;
if(!(dr = opendir(actual_dir)))
{
printf("Error when trying to open dir...");
return 0;
}
/* Lists directory content including file names started with a dot (.) */
while(datum = readdir(dr))
{
/* Jump the actual dir indicator (.) and before dir (..) */
if( (*(datum->d_name) == '.' && datum->d_namlen == 1) // .
||
((*(datum->d_name) == '.' && *(datum->d_name+1) == '.') && datum->d_namlen == 2 ) // ..
)
{
continue;
}
/* Get file data */
stat(datum->d_name, &buf);
printf("%-50s%10d\n", datum->d_name, buf.st_size);
}
printf("\nPress [ENTER] to exit.\n");
getchar();
return 0;
}
struct dirent
{
long d_ino; /* Always zero. */
unsigned short d_reclen; /* Always zero. */
unsigned short d_namlen; /* Length of name in d_name. */
char d_name[FILENAME_MAX]; /* File name. */
};