Thursday, 15 January 2015

c - What does fgets() reads? -



c - What does fgets() reads? -

i have migrate programme unix windows. programme getting info serial port. in original unix machine receiving it, reads as:

char my_buffer_a[200]; memset(my_buffer_a, '\0', sizeof(my_buffer_a)); if (fgets(my_buffer_a, sizeof(my_buffer_a), file_p)) { n = sscanf("%d\t%d\t...%d\n0x0a", my_record.field1, my_record.field2, ...) .... }

i know fgets() read until eof or newline character. means in my_buffer_a, expect '\n' @ end. yet suprise, there also 0x0a! (does mean two newline characters?) going on here?

another thing worry in definition of newline character in unix , windows. not same in unix , in windows. mean after convert, when sscanf, have sscanf("%d\t%d\t...%d\r\n (or sscanf(%d\t%d\t...%d\r0x0d\n0x0a while keeping wierd logic)?

a newline in scanf format string not newline in input -- skips whitespace (any whitespace). same tab characters. in addition, most scanf format specifiers (everything except %c , %[) skip whitespace. in general:

you should never utilize \t, \r or \n in format string (except within %[..]), confuse people. utilize space instead, same thing

you should avoid using redundant spaces in format string, because don't , confuse people.

so above, sscanf phone call becomes:

sscanf("%d%d ...%d 0x0a",

making clear reads 2 numbers, followed 3 periods, followed number, , string 0x0a (4 characters). there may whitespace between of 6 things (or not), , ignored.

as far windows vs unix concerned, windows, there carriage homecoming (\r) characters @ end of lines. these whitespace, , skipped scanf other whitespace. can ignore them.

c windows unix newline fgets

No comments:

Post a Comment