Feb 7, 2015 at 5:54am UTC
Hi, I am trying to write my own version of the "ls" command which lists hidden files I would very much appreciate it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <sys/types.h>
#include <dirent.h>
using namespace std;
int main(void ) {
stru
struct dirent *ent;
while ((ent = readdir(dir)) != NULL)
{
cout << (ent->d_name)
else
{
cout << "Error opening directory" << endl;
}
return 0;
}
Last edited on Feb 9, 2015 at 5:59pm UTC
Feb 7, 2015 at 2:24pm UTC
> any file starting whose first character is "." will not be listed
check the first character of the filename
Feb 7, 2015 at 8:34pm UTC
Like i said, its a .file I am trying not to show that unless i do prog1-h ..
Feb 8, 2015 at 7:56pm UTC
Why not use the != operator?
(ent->d_name[0] != '.' )
Feb 9, 2015 at 3:45am UTC
Hi, I fixed it ehhe. So i added another argument besides the -h. Now if the user does a ./program folder1 folder 2 it shows the files in those folder. Code below:
[code]#include <stdio.h> /* printf */
#include <stdlib.h> /* system, NULL, EXIT_FAILURE */
#include<iostream>
#include <dirent.h>
using namespace std;
int main (int argc, char **argv)
{
DIR* dir = opendir(".");
struct dirent* ent;
Last edited on Feb 9, 2015 at 6:00pm UTC