System call program error

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
> any file starting whose first character is "." will not be listed
check the first character of the filename
Like i said, its a .file I am trying not to show that unless i do prog1-h ..
1
2
if( first_character( ent->d_name ) not_eq '.' )
   std::cout << ent->d_name << '\n';
hi, I actually did it like this

1
2
3
if  (!(ent->d_name[0]== '.' ))  {
				
	    cout << (ent->d_name) << endl;



now I am trying to say hey if the user does a program1 -h show the hidden files

if( string(argv[1]) == "-h" ) && argc==2 just gives me a segment fault
Why not use the != operator?

(ent->d_name[0] != '.' )
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
Topic archived. No new replies allowed.