samedi 18 avril 2015

Reading a file word by word with no ending chars

I have this small program in C that reads through a file and compares word by word, how can I assure that strings like "this," won't be read as a word? I would like it to read as "this"



if(argc != 3)
{
printf("Usage: ./sw <word> <filename> \n");
exit(1);
}

char* word = argv[1];
const char* filename = argv[2];

FILE* file = fopen(filename, "r");
if(file == NULL)
{
printf("Could not open file\n");
exit(1);
}

//Assuming one word can not have more than 250 chars
char w[250], check_eof;

do
{
check_eof = fscanf(file, "%s", w);
if(strcmp(word, w) == 0)
{
printf("W : %s \n", w);
}
} while(check_eof != EOF);

fclose(file);

return 0;

Aucun commentaire:

Enregistrer un commentaire