top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

WAP in C to print all the files present in a directory

+2 votes
357 views

I want to make my own "ls" command, without using "system" library function

posted Feb 2, 2015 by Chirag Gangdev

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

Try something like this (pass the directory name as argument i.e. $a.out )

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>

int main(int argc, char* argv[])
{
    DIR *mydir;
    struct dirent *myfile;
    struct stat mystat;

    mydir = opendir(argv[1]);
    while((myfile = readdir(mydir)) != NULL)
    {
        printf(" %s\n", myfile->d_name);
    }
    closedir(mydir);
}
answer Feb 2, 2015 by Salil Agrawal
Similar Questions
+2 votes

Write a C program which accept two strings and print characters in second string which are not present in first string?

Example:
String 1: apple
String 2: aeroplane

output:
ron

+2 votes

Input:
arr = {'a', 'b'}, length = 3

Output:
aaa
aab
aba
abb
baa
bab
bba
bbb

0 votes

I want to write a c program where I can count no of line, no of blank lines, no of commented lines and no of lines ending with semicolon, please help!!!

...