-1

Run command sequence:

gcc -Wall main.c -o a.out

./a.out < inputfile.txt

I want to read from file like ./a.out < inputfile.txt and after iterating char by char, print OK if all the characters are alphanumeric or ERROR otherwise. I can't seem to get this to work.

inputfile.txt

the brown fox jumped over the dog

this is another string

here is another string

main.c

int main() {

char c;

while (!feof(stdin)) {
    c = getchar();
    if (!isalnum(c)) {
        printf("ERROR!\n");
        
        exit(1);
    }
    }
printf("OK\n);

return 0;
}
Community
  • 1
  • 1
Ryan
  • 354
  • 3
  • 18
  • 1
    What output do you get, and how does that compare to what you expect? "can't seem to get this to work" doesn't help us much to understand the problem you're having. – Simon Sep 01 '15 at 20:38
  • Well, I get ERROR when I want OK.. – Ryan Sep 01 '15 at 20:43
  • currently inputting from the file a single line that says "asdf" – Ryan Sep 01 '15 at 20:43

3 Answers3

3

As the others said - spaces and new lines are not alnum characters. Use isalnum() + isspace() in this case. In addition to that consider using some kind of a flag instead of using exit() function:

#include <stdio.h>
#include <ctype.h>

int main()
{
    int c;
    char ok='Y';

    while(c = getchar())
    {
        if(c == EOF) break;

        if(!isalnum(c) && !isspace(c))
        {
            ok = 'N';
            break;
        }
    }

    printf("%c\n", ok);
    return 0;
}

RTFM: http://www.cplusplus.com/reference/cctype/
I swear this is the last time when I'm gonna help out people who can't even debug their codes.

mtszkw
  • 2,717
  • 2
  • 17
  • 31
2

A space is not an alphanumeric character.

See the table in this page for what is or isn't "isalnum".

Amit
  • 45,440
  • 9
  • 78
  • 110
  • CR & LF are also non-alphanumeric – Amit Sep 01 '15 at 20:45
  • Which would that be? I literally have one line of code in the input text that is "asdh" no quotes. – Ryan Sep 01 '15 at 20:47
  • Use a debugger, see EXACTLY which value fails you, and if you can't figure that out, provide more details in your question – Amit Sep 01 '15 at 20:48
2

You're using feof wrong. feof returns true when the EOF indicator has already been set, i.e: when you have already read an EOF. So when stdin reaches end-of-file you still get one loop iteration with EOF, which isn't an alpha-numeric character. In order to make sure you can properly distinguish EOF from any valid character you should declare c as an int. I suggest:

int c = getchar();
while(c != EOF){
    if(!isalnum(c)){
        printf("ERROR!\n");
        exit(1);
    }

    c = getchar();
}

printf("OK\n");
Community
  • 1
  • 1
Kninnug
  • 7,992
  • 1
  • 30
  • 42
  • So where is stdin being utilized. – Ryan Sep 01 '15 at 20:49
  • 1
    @Ryan it is implicit in `getchar`, you can also use `fgetc(stdin)` – Kninnug Sep 01 '15 at 20:50
  • Hmm, well when running this code, it doesn't do anything when I feed in the input file. It stalls as if it is waiting for input – Ryan Sep 01 '15 at 20:51
  • @Ryan if you're feeding the input in the terminal (rather than feeding an input file on the command line) you'll need to end the input with CTRL+D (or CTRL+Z on Windows). – Kninnug Sep 01 '15 at 20:54
  • In terminal in Linux I run gcc -Wall main.c -o a.out, then I go ./a.out < input.txt after pressing cntrl +D nothing happens still. – Ryan Sep 01 '15 at 20:55
  • Detail: OP is not strictly "using feof" wrong, although code's usage is pointless. Calling `feof()` has well defined behavior immediately after being opend. `EOF` ( a negative value like -1) often unfortunately _does_ fit in a `char` when the `char` is signed. That overlaps _some_ `char` though. Of course you are right with `int c`. +1 – chux - Reinstate Monica Sep 01 '15 at 21:16
  • @chux I didn't mean to imply that the use of `feof` would invoke UB or some such, but that his usage would mean the loop will always get an EOF value and thus always print `ERROR`. Edited my answer to clarify that. – Kninnug Sep 01 '15 at 22:37