1

I am trying to read from file. After reading, I want to display it's contents. My program looks like :

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


int main(int argc, char const *argv[])
{
    // 
    char array_of_input[10][10];
    FILE *fp;
    fp = fopen("input.txt","r+");
    int i = 0,j = 0;
    char ch;

    while ( !feof(fp))
    {
        ch = (char)fgetc(fp);
        printf("%c\n", ch );
        if ( ch == " ")
        {
            continue;
        }
        else if ( ch == "\n")
        {
            i++ ;
            j = 0 ;
        }
        else
        {
            array_of_input[i][j] = ch;
            j++;
        }
    }
    return 0;
}

But, I am getting error:

ISO C++ forbids comparison between pointer and integer [-fpermissive] if ( ch == " ")

fgetc(fp) returns int and then it's type-cast to char. I can't see any integer here.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
learner
  • 4,614
  • 7
  • 54
  • 98
  • 3
    1) `( ch == " ")` --> `( ch == ' ')` 2) Same for `( ch == "\n")`, use `int ch`, `feof()` use wrong, limit input to 9, (add null character termination maybe.) ... Open to anyone to provide details. – chux - Reinstate Monica Sep 07 '15 at 18:53
  • 1
    You are using the `feof()` incorrectly. You should have `int ch; while((ch = fgetc(fp)) != EOF) {...}` Note the `int ch`. Please read the man page for `feof` and see http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – Weather Vane Sep 07 '15 at 19:06
  • @WeatherVane thank you for pointing it out. I was having an issue because of it. – learner Sep 07 '15 at 19:11
  • I could be wrong, but the only reasons for using a `char` type are when you want an array of 1-byte elements, or a packed `struct`. Notice how the library functions that you would think would take or return a `char` type (`isupper`, `getch`, `strchr` and so on) don't. They take/return an `int` type. – Weather Vane Sep 07 '15 at 19:17
  • @WeatherVane this is a relict from times when C didn't have prototypes. IMHO there's nothing wrong with using `char` when you mean `char`. –  Sep 07 '15 at 19:30
  • @FelixPalmen so are you saying that you should use a `char` type for the return value from `getch`? – Weather Vane Sep 07 '15 at 19:31
  • @WeatherVane probably not if you don't like casting ;) Just saying the reason it returns `int` is backwards compatibility with K&R C. –  Sep 07 '15 at 19:33
  • @FelixPalmen I disagree. *"The behavior of isupper and _isupper_l is undefined if c is not EOF or in the range 0 through 0xFF, inclusive.*" The `char` type cannot hold the value `EOF`. – Weather Vane Sep 07 '15 at 19:33
  • How would you cast `EOF` returned from `getch()` and distinguish it from the byte value `0xFF`? – Weather Vane Sep 07 '15 at 19:35
  • @WeatherVane yes, this is a special case. I still claim it's an historic relict. What you would do is most of the time check for EOF, and if not, cast to `char`. I'm pretty sure this function would have been defined differently if prototypes were available back then, but of course, this is just my assumption. –  Sep 07 '15 at 19:35
  • @FelixPalmen no, you would use the `int` type in the first place. – Weather Vane Sep 07 '15 at 19:36
  • @WeatherVane the `int` doesn't make much sense from a design POV. It's there because "back then" every function returned `int`. The possibility to represent something like EOF is more like an accidental benefit. You can have a different opinion, for ultimate clarity, you'd have to ask the original designers ;) –  Sep 07 '15 at 19:39
  • @FelixPalmen the `char` type make a lot of sense with embedded processors with niggardly amount of memory, but its best just to use the natural size. That will conform with the libraries, whatever their origin might be. – Weather Vane Sep 07 '15 at 19:41
  • @WeatherVane this is a typical *performance consideration* and a good compiler will solve *this* problem using alignment. (and that said, I'm really in favour of using the natural machine size wherever I don't need something different for logical OR technical reasons) –  Sep 07 '15 at 19:41

1 Answers1

2

ch is a char - you should compare it to a char literal (denoted by single quotes), not a string literal (denoted by double quotes):

if ( ch == ' ')
{
    continue;
}
else if ( ch == ' ')
{
    i++ ;
    j = 0 ;
}
else  // rest of code snipped
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Thank you for the help. Can you explain the reason of the error : **comparison between pointer and integer** – learner Sep 07 '15 at 18:59
  • @learner you should read about C "strings" ... the catch is, C does not know strings (C++ knows `std::string`, but that's something different) -- so in C a "string" ist just an address of a sequence of characters. And this is how string literals work in C and C++ ... `"A"` is "seen" by the compiler as a byte sequence 65, 0 (the end of a string is a 0 byte) and evaluates to the address where the first byte is stored --> it is a pointer. –  Sep 07 '15 at 19:01
  • On the other hand, `'A'` is just another way of writing `65` ;) -- so THIS is an integer [of type `char` --- meaning exactly one byte] –  Sep 07 '15 at 19:06
  • And on a side note ... your posted code looks really like C, not C++ ... you should translate it using a C-compiler :) –  Sep 07 '15 at 19:11
  • 1
    @FelixPalmen `'A'` is only 65, if ascii is used. `'A'` is an `int` in C (`sizeof('A')==sizeof(int)`), it is a `char` in C++. – mch Sep 07 '15 at 19:14
  • @mch ... no need to overcomplicate it, or do you want to explain int propagation etc here? learner will learn ;) besides, he used a C++ compiler ... ;) –  Sep 07 '15 at 19:19
  • @FelixPalmen Learner is learning. I am using sublime to compile programs. I got options for c++ compilers only. – learner Sep 07 '15 at 19:33
  • @mch is right, character constants have type int and not char as stated by felixpalmen. Maybe he is learning but better teach him right then. The question is clearly labeled as C, not C++. – jforberg Sep 07 '15 at 22:56