2

Why does this not compile? Cant see the error

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

int main(void)
{
    char *c;
    FILE *f = fopen("file.txt", "r");

    if(f == NULL) {
        printf("Could not open file");
    }

    while((c = fgetc(f)) != EOF) {
        if(strcmp(c, " ") == 0) {
            printf(" ");
        } else if(strcmp(c, ":") == 0) {
            printf(":");
        } else if(strcmp(c, "@") == 0) {
            printf("@");
        } else if(strcmp(c, "\n") == 0) {
            printf("\n");
        } else {
            printf("Not a valid char");
    }
}

}

codaddict
  • 445,704
  • 82
  • 492
  • 529
user265767
  • 559
  • 3
  • 12
  • 27
  • 5
    "Cant see the error" ? You mean your compiler crashes as badly on this code that you even can't tell us what he is complaining about? – Jens Gustedt Sep 20 '10 at 15:48
  • You must return an int, if you set the return type of main as int. –  Sep 20 '10 at 17:01
  • 1
    @crypto: if he's using a C99 compiler, there's an implicit `return 0;` statement right before the end of `main`, so he needn't type it explicitly (but, it's better if he does, I think) – pmg Sep 20 '10 at 17:28

4 Answers4

11

fgetc returns the char currently at the file pointer as an integer.

So char *c; should be int c;

and

if(strcmp(c, " ") == 0) {

should be

if(c == ' ') {

and similarly change other comparisons.

You can compact the comparisons as:

while((c = fgetc(f)) != EOF) {
    if(c == ' ' || c == ':' || c == '@' || c == '\n') {
        printf("%c",c);
    } else {
        printf("Not a valid char");
    }
}
codaddict
  • 445,704
  • 82
  • 492
  • 529
0

Because a char * is not a char.

The fgetc function returns a character, not a string. That means that your entire group of if statements is wrong too. You should be doing simple things like:

if( c == ' ' ) {
} else if( c == ':' ) {
} ...
Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
0

Yes, fgetc() returns int, not char or char*. Why is this important? Because EOF is usually (always?) defined as -1. If fgetc() returns EOF into an 8 bit char, it will be represented as 0xFF. In some character sets this is a valid character. e.g. y-umlaut in ISO-8859-1. Thus using

char c; // << this is wrong, use int
while((c = fgetc(aFile)) == EOF)
{
     // stuff
}

you cannot distinguish between end of file and one of the characters that can legitimately appear in the stream.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • Why the down vote? The code sample was to demonstrate how **not** to do it. Please fully read the answer before down voting. – JeremyP Sep 21 '10 at 08:16
-1

As mentioned, fgetc returns an int not actually a string (So strcmp will fail). My personally preferred though not really any different method of char comparison is to use a switch, and since you have several printing out the input character you might have something like:

while( (c = fgetc(f)) != EOF ) {
  switch( c )
  {
    case ' ':
    case ':':
    case '@':
    case '\n':
      printf( "%c", c );
      break;
    default:
      printf( "Not a valid char" );
  }
}

I have found this to be the easiest way, especially when you know you'll want to expand on your conditions later. ( Say if you wanted to add: 'f', 'o', and 'r' )

Ian Lee
  • 502
  • 1
  • 5
  • 12