Exactly how you do this, may depend on your OS. This is a rather generic approach for Windows and with a bit of touch up perhaps, for *nix as well.
This program was written in Pelles C (a Windows only compiler), and requires that the "Define compatibility names option" be checked in the project options. I have no idea how that translates into VC++.
It searches for a file in a directory, but doesn't print them out - which is trivial to change.
Note that non-text files should not be opened and printed out!
Code:
/*Remember to go into project options and check the "Define compatibility names options box.
That gets rid of the "Target Architect not defined", error.
*/
#include <dirent.h>
#include <stdio.h>
#include <string.h>
int main(void) {
int i=0;
DIR *dir;
struct dirent *direntry; //could be a file, or a directory
dir = opendir("C:/Users/Adak");
if(!dir) {
printf("Error: directory did not open!\n");
return 1;
}
while((direntry=readdir(dir))!=NULL) {
if(++i < 20)
printf("%s\n",direntry->d_name);
if((strcmp(direntry->d_name, "test.txt"))==0) {
printf("\nThe %s file has been found\n",direntry->d_name);
i=-99; //just a flag value to show the file was found
break;
}
}
if(i!=-99)
printf("\nThe test.txt file was not found\n");
closedir(dir);
printf("\n");
return 0;
}
You'll need to read up on using the command line options as input for a C program, and make some other changes as well. Don't think of this as the program you want, served up on a platter - it is just a starting point for you.