I am currently doing a C program, which very simply takes a file as input, and iterates over the file and checks if the character 'a' or 'A' exist inside the file. If no a's are found it terminates, otherwise it prints a message. The a's here are simply for show, I try to make a simple program that can search for any char parameter (and later extend to integers etc.)
The program is as follows:
int ReadFile(char* argv[]) {
FILE *fp;
char x = 'a';
char y = 'A';
fp = fopen(argv[1], "r");
if (!fp) { // checks if the file exists
printf("File does not exist!\n");
return 1;
}
while (!feof(fp)) { // keeps reading until file end of file (feof)
if (*fp == x || *fp == y) {
printf("no A's are allowed!\n");
return 1;
}
}
fclose(fp); // always close file after use
printf("Ok\n");
return 0;
}
The problem however is currently I am trying to compare a fopen
with a char
, with:
if (*fp == x || *fp == y) {
printf("no A's are allowed!\n");
return 1;
}
which does not work for obvious reasons.
My question is how do I either cast a fopen
to a char
, so the file pointer returns a char
that I can evaluate upon - or how can I solve this issue otherwise?