I've got some code which reads lines from a txt file and then does adds to a database.
If you type data straight into the txt file, then this is sufficient to recognise a newline: if (ch == '\n')
However, if you cut & paste out of Microsoft Word, checking for \n
doesn't work.
If I dump out the hex values / characters one by one, the actual data looks like this:
2e . [ Last char of line ]
d
58 X [ First char on next line ]
The full stop is the last character on one line. The 'X' is the first character on the next line. The hex 'd' causes the newline.
What is going on? And how can I test my ch
variable against > d<
, given that it's space-d in the hex?
Thanks.
Windows uses a pair of characters to end a line: a carriage return (0x0d) followed by a line feed (0x0a).
You can write these in C as '\r'
and '\n'
, respectively.
Hexadecimal literals in C are prefixed with 0x
or 0X
, so in your case you could use 0xd
or 0XD
or 0xD
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With