[C Language] Read file name inside a directory

Source code :

#include <stdio.h>
#include <dirent.h>

void main(void){

	//choose the folder path
	char DIRPATH[]="c:\\images\\";
	DIR *dir;
	struct dirent *entry;

	dir=opendir ( DIRPATH );

	//checking the folder content
	while ( NULL != ( entry = readdir ( dir ) ) ) {
		DIR *test;
		char filePath[260];
		sprintf ( filePath,"%s%s", DIRPATH, entry->d_name );
		errno=0;
		test=opendir ( filePath );

		if (test==NULL && errno!=0){
			printf("%s\n",entry->d_name);
                       // ... (your source code)
                       // ...
                       //...
		} else {
			closedir(test);
		}
	}
	closedir(dir);
}

Attention!! (Penting)
Ketika compiler gagal meng-compile kode dan keluar pemberitahuan error seperti berikut :

‘dirent.h’: No such file or directory

berarti dalam folder ‘include’ tidak terdapat header file bernama “dirent.h”

Please download the file from  = http://www.softagalleria.net/download/dirent/
download file, unzip the package and copy the “dirent.h” to C:\Program Files\Microsoft Visual Studio 9.0\VC\include
Target file path bisa berbeda tergantung versi dari Visual Studio yang digunakan.
(Saya menggunakan Visual Studio 2008)

Selamat mencoba!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: