0

So I'm currently trying to read a input file character by character and I'm trying to see when a new line occurs. I get all the characters fine, but instead of a '\n' for a new line I get 'á' or -97 if I cast it to an int. Here's my code and I'm using VS 2015.

int main(void) {
    FILE *fp;
    fp = open_input_file();
    if (fp != 0) {
        char ch = read_character(fp);
        int d = (int)ch;
        while (ch != EOF) {
            printf("%d\n", d);
            ch = read_character(fp);
            d = (int)ch;
        }
    }
    getch();
    return 0;
}
char read_character(FILE *infile) {

    int c;
    c = getc(infile);

    return (char) c;
}
Luke
  • 11
  • 1
  • 6
  • what do you want to ask? could you be more clear? – Flying disk Jun 15 '16 at 18:44
  • 2
    You keep casting to and from `int`. Just stick with `int`, as returned by `getc`, for the function type too.. – Weather Vane Jun 15 '16 at 18:50
  • Just think twice want it could mean that you need to use the casting hammer here: `return (char) c;`. A general rule of thumb in C: If you see yourself confronted with the need to cast your are either dump or smart. Normally you need to cast only under *very rare* circumstances. – alk Jun 15 '16 at 19:12
  • 1
    `getc()` returns an `int` by intention. Just not ignore this. – alk Jun 15 '16 at 19:17
  • 1
    1) Insure `char read_character(FILE *infile)` is declared/defined before usage in `main()`. 2) Post content of the input file. 3) post output seen – chux - Reinstate Monica Jun 15 '16 at 19:24
  • this line: `while (ch != EOF) {` may or may not work, depending on if your C compiler implements a `signed char` or an `unsigned char` (due to sign extension if a signed char.) Strongly suggest: 1) return an `int, from `read_character()` rather than a `char`. Note: `EOF` is `-1` which is an `int`, not a `char`. – user3629249 Jun 16 '16 at 07:18
  • the posted code: 1) is missing the `#include` statements. Are we expected to guess what the OP actually included? 2) The prototype for `read_character()` is missing, so the compiler will ( depends somewhat on the version of the C standard used) assume all passed parameters and returned value are `int` where the function is called. – user3629249 Jun 16 '16 at 07:21
  • a `\n` is not a printable character. Strongly suggest using `%x` to print it. – user3629249 Jun 16 '16 at 07:23

1 Answers1

0

You can detect a newline with minor changes to your existing code:

int main(void) {
    FILE *fp;
    fp = open_input_file();
    if (fp != 0) {
        int ch = read_character(fp);//change to int return (getc() returns int)
        if(ch == '\n') //A simple comparison here will detect newline
        {
            printf("Found Newline: %d\n", ch);
        }
        ///int d = (int)ch;// not necessary with current changes
        while (ch != EOF) {
            //printf("%d\n", d);
            ch = read_character(fp);
            if(ch == '\n')//Note: ASCII for \n is value 10, or hex A
            {
                printf("Found newline: %d\n", ch);
            }

            //d = (int)ch;
        }
    }
    getch();
    return 0;
}

int read_character(FILE *infile) 
{  //changed prototype to return int

    int c;
    c = getc(infile);

    return c;//modified return value to int
}
ryyker
  • 22,849
  • 3
  • 43
  • 87