-1

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?

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
NewDev90
  • 379
  • 2
  • 21
  • 2
    You are misusing `feof`. It returns whether **an earlier read** set the EOF flag on the handle. – ikegami Sep 02 '19 at 15:26
  • 3
    You never read from the file!!! `fgetc` – ikegami Sep 02 '19 at 15:27
  • 2
    Possible duplicate of [Reading a file one char at a time](https://stackoverflow.com/questions/12269125/reading-a-file-one-char-at-a-time) – user202729 Sep 02 '19 at 15:29
  • @ikegami Hmm can you elaborate? I've been using this technique so far for programs that reads a file and ouputs to the terminal, which similar setup, and it works fine? – NewDev90 Sep 02 '19 at 15:29
  • 4
    You need to get a good beginners book or tutorial about C, because that's not how you read from a file. – Some programmer dude Sep 02 '19 at 15:30
  • 2
    Also read [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Some programmer dude Sep 02 '19 at 15:31
  • @KenLar forget this tutorial (which one BTW?). There are a lot of crap tutorials out there. – Jabberwocky Sep 02 '19 at 15:37
  • 1
    @KenLar Obviously Someprogrammerdude intends to point out that *checking for `feof(file)` is usually wrong* and this is *explained in the link*... – user202729 Sep 02 '19 at 15:39
  • OT: `int ReadFile(char* argv[])` is strange, it should probably be `int ReadFile(const char *filename)... fopen(filename, "r");` and call it like this: `ReadFile(argv[1])` after checking that `argc >= 2`. – Jabberwocky Sep 02 '19 at 15:43
  • @Jabberwocky I'ts written inside the main function, I prob just forgot to change this when I made a seperate function :) thanks for pointing out. In regards to the toturials it was a 50-split toturial on Youtube, cant remember the name to be hornest, but it was that signature that was used to iterate files throughout several videos. – NewDev90 Sep 02 '19 at 15:52

1 Answers1

1

A "file pointer" like fp is not a pointer to the file's contents. It is a "handle" or "descriptor" which allows you to read characters (and perform other operations) on a file which you have opened.

You need to use a function like getc to read characters:

c = getc(fp);

So an improved version of your program might look like

#include <stdio.h>
#include <string.h>
#include <errno.h>

int main(int argc, char *argv[]) {
    FILE *fp; 
    int c;

    fp = fopen(argv[1], "r");

    if (!fp) {
        printf("Can't open file %s: %s\n", argv[1], strerror(errno));
        return 1;
    }

    while ((c = getc(fp)) != EOF) {
        if (c == 'a' || c == 'A') {
            printf("no A's are allowed!\n");
            return 1;
        }
    }

    fclose(fp); // allways close file after use

    printf("Ok\n");

    return 0;
} 

The big difference is that I'm not calling getc to read characters from fp and storing them in a variable c. (Note also that c is declared int, not char as you might expect. This is important.)

Other improvements:

  1. I got rid of the call to feof. See "Why is while ( !feof (file) ) always wrong?" I replaced it with while((c = getc(fp)) != EOF), which is admittedly rather obscure at first, but it's a classic C idiom which you'l need to learn soon enough. (I understand what you thought the feof code did, and why you thought it worked, but trust me, it's no good.)

  2. Files can fail to open for reasons other than that they don't exist. I added a call to strerror to explain why the file-open might have failed.

  3. I got rid of the x and y variables, which you didn't really need.

  4. I renamed ReadFile to main to make it a standalone program.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
  • That is exactly was I was looking for, thanks alot! solved my problem. Easy to understand, I read about the getc function but did not think about using it to convert the fp to what I was looking for. – NewDev90 Sep 02 '19 at 15:38
  • Thanks for explaining, I have not yet worked with strerror, but its nice to know in advance how I can treat any potential errors when trying to open af file. – NewDev90 Sep 02 '19 at 15:54