Member Avatar for Eonimal

Hello!

I am trying to make a mimic code of LS command in C.

It is compiled with gcc in ubuntu, and I have some questions to ask.

I have used opendir to open current directory, and then i read each directory using while loop to print all the directories inside that directory.
somehow it is showing .. and . as a directory.

I know .. is a back directory thingy but i dont need this to be outputted since it
is not what i need.


ive tried if ( directory != "..") but still outputs it...

is there any way to do this?

ill attach the code.

int main()
{
  char *example;
  example = (char*)malloc(1024);
  example = get_current_dir_name();/* this makes some error, change it later to cwd() function so that it wont make any errors.*/
  printf("test\n");
  DIR *dit;
  struct dirent *dip;
  dit = opendir(example);
  while ( ( dip = readdir(dit) ) != NULL )
  {
    char *temp;
    temp = dip->d_name;
    printf( "%s\n", temp);
    
  }
  //recursive_read_dir(example);
  
  return 0;
}
Member Avatar for Eonimal

Solved it .

Used strcmp function to compare.

guess directory != ".." is different with strcmp(directory, ".." ) != 0 . LOL

Hello!

I am trying to make a mimic code of LS command in C.

It is compiled with gcc in ubuntu, and I have some questions to ask.

I have used opendir to open current directory, and then i read each directory using while loop to print all the directories inside that directory.
somehow it is showing .. and . as a directory.

I know .. is a back directory thingy but i dont need this to be outputted since it
is not what i need.


ive tried if ( directory != "..") but still outputs it...

is there any way to do this?

ill attach the code.

int main()
{
  char *example;
  example = (char*)malloc(1024);
  example = get_current_dir_name();/* this makes some error, change it later to cwd() function so that it wont make any errors.*/
  printf("test\n");
  DIR *dit;
  struct dirent *dip;
  dit = opendir(example);
  while ( ( dip = readdir(dit) ) != NULL )
  {
    char *temp;
    temp = dip->d_name;
    printf( "%s\n", temp);
    
  }
  //recursive_read_dir(example);
  
  return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.