Fgets and non-printable characters
Can fgets read non-printable characters into a given char * (I can't seem to)? And if not, what alternative would allow the maximum number of input characters from the stream to char *?
EDIT (for my specific case)
I have an encoder that prints "Le \ D7" to stdout, which is passed to the decoder, which fetches it from its stdin using:
if( fgets( inputChars, MAX_BYTES_IN, stdin ) == NULL )
{
fprintf( stderr, "Trouble getting input\n" );
return 0;
}
while( inputChars[crntChar] != '\0' && inputChars[crntChar] != '\n' )
{
printf( "Value %d: %d\n", crntChar, inputChars[crntChar]);
crntChar++;
}
This leads to:
Value 0: 76
Value 1: 101
Value 2: -41
Using fgetc has the same result
source to share
You get a weird value because of the unsigned integer conversion.
char x = 198;
printf("x = %d\n", x);
printf("(unsigned) x = %u\n", (unsigned) x);
printf("(unsigned char) x = %d\n", (unsigned char) x);
Output:
x = -58 (unsigned) x = 4294967238 (unsigned char) x = 198
Listing (unsigned char)
is what you want.
Please ignore the signed overflow in my code. Note that if you compile GCC and the flag -funsigned-char
, the output will be as follows:
x = 198 (unsigned) x = 198 (unsigned char) x = 198
source to share
The easiest way is to use fgetc()
. fgets()
relies on fgetc()
.
But there are many alternatives, fread()
is one of them. fscanf()
...
fgetc()
while others read both printable and non-printable characters into an array char
. A char
is just 1 byte number encoded in ASCII (or 2 bytes in the case wchar_t
). In C.
no concept of symbols
printable
and
non printable
source to share