You're allocating memory for test
, but that doesn't fill it with the directory info. readdir
allocates the memory for the dirent
structure for you, so it isn't even necessary. Assign the return-value of readdir
to test
instead:
while((test = readdir(dir)) != NULL){
printf("%s\n", test->d_name);
// the man page says you may not free the dirent
}