# Optimization of showing dir contents function

This topic is 5135 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, I wrote a code to show the contents of a directory + file size in bytes. Ok, no problem with it. I just want to know if it is possible to write a shorter code to jump the "." and ".." as you can see in my if: /* Jump the actual dir indicator (.) and before dir (..) */. Thanks Alfred
#include<dirent.h>
#include<stdio.h>
#include<sys/stat.h>

const char *actual_dir = "D:/Programacao/Aprendizagem/C - Completo e Total/cap9/";

int main()
{
DIR *dr;
struct dirent *datum;
struct stat buf;

if(!(dr = opendir(actual_dir)))
{
printf("Error when trying to open dir...");
return 0;
}

/* Lists directory content including file names started with a dot (.) */
{
/* Jump the actual dir indicator (.) and before dir (..) */
if( (*(datum->d_name) == '.' && datum->d_namlen == 1) // .
||
((*(datum->d_name) == '.' && *(datum->d_name+1) == '.') && datum->d_namlen == 2 ) // ..
)
{
continue;
}

/* Get file data */
stat(datum->d_name, &buf);

printf("%-50s%10d\n", datum->d_name, buf.st_size);
}

printf("\nPress [ENTER] to exit.\n");
getchar();
return 0;
}


The struct dirent as you see in "dirent.h":
struct dirent
{
long		d_ino;		/* Always zero. */
unsigned short	d_reclen;	/* Always zero. */
unsigned short	d_namlen;	/* Length of name in d_name. */
char		d_name[FILENAME_MAX]; /* File name. */
};



##### Share on other sites
Well, if you changed to using c-style nul-terminated strings (why are you storing the length seperately? is the string not nul terminated already?), then you could use strcmp

##### Share on other sites
Oh! strcmp... I have had forgot it.
Thanks Extrarius! Rate ++ to you.

why are you storing the length seperately?
I'm using the C standart library, it stores.

I just changed the if to:
        /* Jump the actual dir indicator (.) and before dir (..) */        if(!strcmp(datum->d_name, ".") || !strcmp(datum->d_name, ".."))        {            continue;        }

and it works perfect as the before sentence.

##### Share on other sites
So does that code actually work? Because if it does, then that means that the strings are null-terminated, which makes it alot easier to compare them with other strings.

##### Share on other sites
Quote:
 Original post by bytecoderSo does that code actually work? Because if it does, then that means that the strings are null-terminated, which makes it alot easier to compare them with other strings.

Yes, both codes worked well.

##### Share on other sites
BTW, unless you're "optimizing" for source code length, the strcmp version might be worse for you because it is actually larger in terms of (machine)code size. But it does look prettier.

##### Share on other sites
Quote:
 BTW, unless you're "optimizing" for source code length, the strcmp version might be worse for you because it is actually larger in terms of (machine)code size. But it does look prettier.

Not just prettier, but more readable, too; which is much more important than optimizing for speed considering the fact that you usually read code more often than you write it.

##### Share on other sites
Yes, my intention was to reduce the source code, not the object code.

Thanks.

##### Share on other sites
Quote:
Original post by bytecoder
Quote:
 BTW, unless you're "optimizing" for source code length, the strcmp version might be worse for you because it is actually larger in terms of (machine)code size. But it does look prettier.

Not just prettier, but more readable, too; which is much more important than optimizing for speed considering the fact that you usually read code more often than you write it.
Well, you usually run code more often than you read it or write it, so speed is pretty important.

##### Share on other sites
    /* Lists directory content including file names started with a dot (.) */    while(datum = readdir(dr))        if(strcmp( datum->d_name, ".") && strcmp( datum->d_name, ".."))        {	        /* Get file data */	        stat(datum->d_name, &buf);	        printf("%-50s%10d\n", datum->d_name, buf.st_size);        }

cheers!

1. 1
2. 2
3. 3
Rutin
22
4. 4
frob
17
5. 5

• 9
• 33
• 13
• 13
• 10
• ### Forum Statistics

• Total Topics
632580
• Total Posts
3007194

×