I am working on a small C program for a college assignment, and I noticed a strange error in my code. I use an iMac with a short keyboard, but its battery was flat, so I plugged in a standard USB keyboard with a numeric keypad.
Itβs strange that if I press [Enter] on my numeric keypad, it seems to do what the regular [Enter] key does, but \ n I try to detect in the stdin function that I did to read the keyboard input, it does not work when I am using the numeric keypad [Enter].
Wtf?
Here is my function that reads user input:
int readStdin(int limit, char *buffer) { char c; int i = 0; int read = FALSE; while ((c = myfgetc(stdin)) != '\n' && c != '\0') { if (i <= limit) { *(buffer + i) = c; i++; read = TRUE; } } for (i = i; i < strlen(buffer); i++) { *(buffer + i) = '\0'; } return read; } int myfgetc (FILE *fin) { if (fakeStdIn == NULL || *fakeStdIn == '\0') return fgetc (fin); return *fakeStdIn++; }
NB: myfgetc and the following *fakeStdIn are part of the way I can unit test my code and "inject" elements into the stdin stream programmatically, as someone suggested on this question: How to write a test function for another function using stdin input? .
source share