Existe-t-il un moyen de vérifier dans Linux C si un fichier est un fichier texte, par ex. UTF ou ASCII? En bash nous avons la fonction file
. Y a-t-il un équivalent en C?Vérifiez si un fichier est en texte brut dans C
EDIT: Ceci est ma fonction pour valider un fichier texte en C. J'utilise popen
, mais ça ne marche pas très bien. Parfois, j'ai une erreur dans pclose
. Qu'est-ce qui aurait besoin d'être édité dans le code?
int check_file(char *path)
{
FILE *file_type;
char command[] = "/usr/bin/file";
char command_to_execute[512];
char check[512];
int correct = 0;
sprintf(command_to_execute,"%s %s",command,path);
file_type = popen(command_to_execute,"r");
if(file_type == NULL)
{
return correct;
}
fgets(check,512,file_type);
char *pointer;
pointer = strstr(check,"ASCII");
if(pointer != NULL)
correct = 1;
pointer = strstr(check,"UTF");
if(pointer != NULL)
correct = 1;
pclose(file_type);
return correct;
}
Il existe la bibliothèque 'libmagic' qui est utilisée pour implémenter' file'. –
Lire le fichier et voir s'il contient du texte ou non. –
système ("fichier" + nomfichierfichier); – qulinxao