Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to identify a CR and LF

Status
Not open for further replies.

RoyceyBaby

IS-IT--Management
Mar 23, 2001
22
0
0
Hi everybody,

I am reading data from the serial port on my linux machine, I believe I am getting CR/LF for new lines.

My code will treat this as 2 new lines. Is there a way in which I can distinguish between a CR and a LF.

Many thanks,

Royce
 
CR si 10 and LF (I dont remember exactly) is 13 John Fill
1c.bmp


ivfmd@mail.md
 
How can I do this in c, in vb I would usually say

if test = chr(10) then
'CR code here
elseif test = chr(13) then
'LF code here
end if

Many thanks,

Royce
 
CRLF => \r\n bluenote@uyuyuy.com
(excuse my english)
 
if(x == 10)
{
...
}else if(x == 13)
{
...
} John Fill
1c.bmp


ivfmd@mail.md
 
What about '\n'? Doesn't C change (depending on the OS) CR/LF to a '\n'?
 
'\n' is the same as 13 and '\r' is the same as 10. John Fill
1c.bmp


ivfmd@mail.md
 
Just so nobody gets confused...

'\r' is the same as 13 and '\n' is the same as 10

Regards
 
Yes right, but the problems is what they both are 1byte numbers (ie chars), but '\r' , '\n' are char representations of them. No difference for the computer. John Fill
1c.bmp


ivfmd@mail.md
 
what do you mean with 'no difference'??


"hello world\n" is not the same that "hello world\r\n"

bluenote@uyuyuy.com
(excuse my english)
 
no, i mean
"hello world\n\r" is the same thing as
"hello world\x0A\x0D" and it the same thing as
"hello\x20world\n\r" and... you can put insteac of each or all characters its numeric value. John Fill
1c.bmp


ivfmd@mail.md
 
Now wait a minute. In Windows, if I am reading a file, am I going to need to use "\r\n" to represent a newline character? I thought that C would merge them together (to keep programs portable) into a single '\n' character. Is this wrong? Here's why I am thinking this way:

"As we said in Chapter 1, the library implements a simple model of text input and output. A text stream consists of a sequence of lines; each line ends with a newline character. If the system doesn't operate that way, the library does whatever is necessary to make it appear as if it does. For instance, the library might convert carriage return and linefeed to newline on input and back again on output." - The C Programming Language, 2nd Ed. (p. 151)

I'm guessing that newline and linefeed are really the same character (though they do sound like different functions), so this is confusing me. Or, is this only because he is compiling this on a Linux system (which doesn't use CRLF), and the serial port is giving him CRLF for the newlines?
 
the pseudocode:
print_x("xxxxx=\n"); will do \n\r only in consoles and not in all the cases. For example, when you call window(a,b,c,d)(borland specific, see conio.h) and do cprintf, you will go with \n only to new line without going to its beginning. The same is in windows. John Fill
1c.bmp


ivfmd@mail.md
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top