I am trying to read each file in a directory and check if it contains only ASCII characters, which means that if there is even non-ascii character in there it doesn't consider it.
#include <stdio.h>
#include <dirent.h>
int main(int argc , char *argv[]){
DIR *folder;
int files = 0;
struct dirent *entry;
int flag = 0;
int c;
if(argc == 2){
folder = opendir(argv[1]);
}
else{
folder = opendir(".");
}
if(folder == NULL)
{
puts("Unable to read directory");
return(1);
}
else
{
puts("Directory was opened!");
}
while( (entry=readdir(folder)) ){
flag = 0;
FILE *curr_file = fopen(entry->d_name,"r");
while ((c = fgetc(curr_file)) != EOF){
if (c < 0 || c > 127){
break;
flag = 1;
}
}
if(flag == 0){
printf("ASCII file");
}else{ printf("Not ASCII file"); }
}
closedir(folder);
return 0;
}
For some reason though it doesn't work as intended and I can't really figure out why.
question from:
https://stackoverflow.com/questions/65869598/reading-a-directory-and-checking-if-files-are-ascii 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…