Created
February 27, 2020 13:44
-
-
Save PypeBros/a58243437c803183d730c7a3105e9041 to your computer and use it in GitHub Desktop.
why ? don't you trust fgets(3) manpage ?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
int showChar(char c) { | |
switch (c) { | |
case '!'...'~': | |
fprintf(stderr, "'%c ", c); | |
break; | |
case 0: | |
fprintf(stderr, "\\0 "); | |
break; | |
case '\n': | |
fprintf(stderr, "\\n "); | |
break; | |
case -1: | |
fprintf(stderr, "?? "); | |
break; | |
default: | |
fprintf(stderr, "%02x ", c); | |
break; | |
} | |
} | |
int main() { | |
char data[16]; | |
fprintf(stderr, "%p ", data); | |
for (size_t i = 0; i < sizeof(data); i++) { | |
fprintf(stderr, "%02lx-", i); | |
} | |
fprintf(stderr, "\n"); | |
while (!feof(stdin)) { | |
memset(data, -1, sizeof(data)); | |
char* ptr = fgets(data, sizeof(data), stdin); | |
fprintf(stderr, "%p ", ptr); | |
for (size_t i = 0; i < sizeof(data); i++) { | |
showChar(data[i]); | |
} | |
fprintf(stderr, "\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment