This shows another way to do it, without using a system call. Change it around to your own directory and file name, of course.
If you use Pelles C, you need to first click on "Project" tab, project options, and compiler. Check the box for "Define Compatibility Names
Code:
/*Remember to go into project options and check the "Define compatibility names box.
That gets rid of the "Target Architecture 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;
}