0

My Code is HERE

int main(){
  FILE *fp;
  fp = fopen("dic.txt", "r");
  while(getc(fp) != EOF){
    if(getc(fp) == ' '){
        printf("up ");
    }
  }
}

My dic.txt is HERE

dic.txt

my predict is that "up up up up "
because, there are four space " "

but it printed "up " only one

what is problem?

skk
  • 67
  • 4
  • 2
    Post the text file as text in a code block. Not as a picture. – klutt Nov 26 '20 at 16:12
  • @klutt Sorry, forget it, comment deleted. Thanks – Jabberwocky Nov 26 '20 at 16:25
  • regarding: `while(getc(fp) != EOF){ if(getc(fp) == ' '){` This inputs two characters every time through the loop. Suggest: `int ch; while( (ch = getc(fp)) != EOF){ if(ch == ' '){` – user3629249 Nov 26 '20 at 23:13
  • regarding: `fp = fopen("dic.txt", "r");` For robust code, always check (!=NULL) the returned value to assure the operation was successful. Suggest: `fp = fopen("dic.txt", "r"); if( ! fp ) { perror( "fopen to read dic.txt failed" ); exit( EXIT_FAILURE ); }` – user3629249 Nov 26 '20 at 23:16

3 Answers3

3

You are calling getc twice per iteration of the loop; one of these two calls compares the character to EOF, while the other call compares the character to ' '.

This has two consequences:

  • Your program will only print "up" for the spaces which are on even position, and will miss all spaces which are on odd position;
  • Your program might make one extra call to getc after reaching EOF the first time.

How to fix

You need to make a single call to getc per iteration of the loop. Save the character returned by getc to a local variable; then use this variable to check for spaces in the body of the loop, and to check for EOF in the condition of the loop.

Stef
  • 13,242
  • 2
  • 17
  • 28
  • @Jabberwocky What's the point of what? The OP made a mistake when writing their code; two calls to `getc` are done per iteration; only one of these two calls tests for `EOF`, so the first time EOF is reached, it might no be detected and an extra call will be made. – Stef Nov 26 '20 at 16:25
  • Forget it, I deleted my pointless comment in the meantime, you can delete your's too – Jabberwocky Nov 26 '20 at 16:26
  • @Jabberwocky Your comment was not useless. It showed that you misunderstood my explanation. That's a sign that perhaps my explanation was not clear enough. – Stef Nov 26 '20 at 16:27
  • No, it was clearly my reading it wrong. The answer is clear. – Jabberwocky Nov 26 '20 at 16:27
0

You want this:

#include <stdio.h>

int main() {
  FILE* fp;
  fp = fopen("dic.txt", "r");
  if (fp == NULL)
  {
    printf("Can't open file\n");
    return 1;
  }

  int ch;                            // int is needed her, not char !!
  while ((ch = getc(fp)) != EOF) {   // read one char and check if it's EOF in one go
    if (ch == ' ') {
      printf("up ");
    }
  }
}
  • You need to call getc once only in the loop, otherwise you skip one out of two characters.
  • Bonus: you need to check if fopen fails.
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • regarding: `printf("Can't open file\n");` Error messages should be output to `stderr`, not `stdout`. When the error is from a C library function (like 'fopen()') then should also output to `stderr` the text reason the system thinks the error occurred. Suggest calling `perror()` as that outputs both your error message and the text reason. – user3629249 Nov 26 '20 at 23:21
0

Try Out This Code:

FILE *fp;

fp = fopen("dic.txt", "r");
int ch = getc(fp);

while(ch != EOF){
    if(getc(fp) == ' '){
        printf("up ");
    }
}

return 0;
Cristik
  • 30,989
  • 25
  • 91
  • 127