Reputation: 1243
I'm trying to list all folders and all files of a folder with the language C.
This is the following code:
#include <errno.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
int main (int argc, char *argv[])
{
struct dirent *direnp;
struct stat filestat;
DIR *dirp;
if (argc != 2) {
printf("error");
return 1;
}
if ((dirp = opendir(argv[1])) == NULL) {
printf("error");
return 1;
}
while ((direnp = readdir(dirp)) != NULL)
{
stat(direnp->d_name, &filestat);
printf("%s\n", direnp->d_name);
}
return 0;
}
After entering the cmd ./file.c folder
The output from this code is:
folder1
folder2
file1.txt
..
.
file2.txt
I wish to remove this part:
..
.
So the output I wish is:
folder1
folder2
file1.txt
file2.txt
How do I hide the 3 dots?
(Edit: There were some mistakes in the code. I corrected it)
Upvotes: 0
Views: 161
Reputation: 36649
Simply filter them in your while loop:
if (strcmp(direnp->d_name, ".") != 0 && strcmp(direnp->d_name, "..") != 0) {
printf("%s\n", direnp->d_name);
}
Upvotes: 6