-3

I need to print .c files, executables, and directories in seperate color. Please suggest me some ideas. Thanks in advance

int main(void)
{

        DIR *d;
        int iNum = 0;
        struct dirent *dir;
        char *ptr = ".";
        char *ptr1 = "..";

        d = opendir(".");

        if (d)
        {

                while ((dir = readdir(d)) != NULL)
                {
                        if(strcmp(ptr,dir->d_name) && strcmp(ptr1,dir->d_name))
                        {
                                printf("%d\n", dir->d_type);
                                printf("%s\n", dir->d_name);
                                iNum++;
                        }
                }
                closedir(d);
        }
        printf("the number of files are %d\n",iNum);

        return(0);
}
kiner_shah
  • 3,939
  • 7
  • 23
  • 37
bala ece17
  • 19
  • 1
  • pls tell me hoe to use header files efficiently – bala ece17 Jun 13 '17 at 07:44
  • 3
    _Questions seeking debugging help (why isn't this code working?) must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]._ – Sourav Ghosh Jun 13 '17 at 07:45
  • 1
    What is wrong with your code? Does it compile? Does it work as expected or not? BTW: don't use names such as "ptr" or "ptr1", why don't you call them for example "dot" and "twodots" ? – Jabberwocky Jun 13 '17 at 07:58
  • Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named "[What topics can I ask about here?](http://stackoverflow.com/help/on-topic)" and "[What types of questions should I avoid asking?](http://stackoverflow.com/help/dont-ask)". Also please [take the tour](http://stackoverflow.com/tour) and read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – ArturFH Jun 13 '17 at 10:29

1 Answers1

4

You just need to include the required header files.

#include <dirent.h>   // for opendir etc.
#include <stdio.h>   // for printf

The rest of the program is a good start and looks more or less correct to me.

For using colors please look at this SO question.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 2
    Wow, some gutless wonder downvoted without a comment. For what? Answering the question? Amazing how many people forget what it's like to try writing C without even knowing where to begin looking for answers. – Andrew Henle Jun 13 '17 at 09:38