1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
//---------------------------
DIR* FD;
// struct dirent* in_file;
FILE *common_file;
FILE *entry_file;
char buffer[BUFSIZ];
char str[sizeof v]; //v is the second argument of main
strcpy (str,v[1]);
/* Scanning the in directory */
// printf("whats in v[] %s\n",v[1]);
while ((pDirent = readdir(pDir))!=NULL )
{
printf ("[%s]\n", pDirent->d_name);
if (!strcmp (pDirent->d_name, "."))
{
printf("about to continue with '.'\n");
continue;
}
if (!strcmp (pDirent->d_name, ".."))
{
printf("about to continue '..'\n");
continue;
}
// Open directory entry file for common operation
// TODO : change permissions to meet your need!
entry_file = fopen( strcat(str,pDirent->d_name), "r");
if (entry_file == NULL)
{
fprintf(stderr, "Error : Failed to open entry file - %s\n", strerror(errno));
// fclose(common_file);
return 1;
}
else
printf("opened %s succesful. \n",pDirent->d_name );
// Doing some stuff with entry_file :
// For example use fgets
while (fgets(buffer, BUFSIZ, entry_file) != NULL)
{
// Use fprintf or fwrite to write some stuff into common_file
}
// When you finish with the file, close it
fclose(entry_file);
printf ("next d_name is [%s]\n", pDirent->d_name);
memset(str, 0, sizeof str);
strcpy(str,v[1]);
}
// Don't forget to close common file before leaving
// fclose(common_file);
closedir (pDir);
return 0;
}
|