[erlang-questions] : Why ~n instead of n?

Robert Virding <>
Thu Jan 18 23:08:55 CET 2007


Use io_lib to see exactly what characters are emitted:

2> io_lib:fwrite("abc\n", []).
"abc\n"
3> io_lib:fwrite("abc~n", []).
[97,98,99,"\n"]

and you see that they are the same. It is the low-level driver which 
fixes the end-line to be either \n or \r\n as necessary. It also makes 
end-of-line on input be \n removing the \r.

When we added ~n we meant it to be converted at a higher level, but that 
never happened. That is why I said there is no difference.

If this the best solution I don't know, but it would probably break code 
if it was changed, people are used to using \n.

Robert

Raimo Niskanen wrote:
> On Wed, Jan 17, 2007 at 06:16:26PM -0500, Robert Baruch wrote:
> 
>>On Jan 17, 2007, at 5:38 PM, Robert Virding wrote:
>>
>>
>>>Hopefully there will be no lightning strike. :-)
>>>
>>>There was meant to be a difference: \n is the newline character  
>>>while ~n
>>>is causes a system-dependant new line in the current implementation.
>>>There is in fact no difference.
>>
>>OK, so if I were running on a Windows system, ~n should emit \r\n  
>>(i.e. hex 0x0D, 0x0A), and on *nix, \n (i.e. 0x0A). But when you say  
>>"in fact no difference", does that mean that although ~n *should*  
>>emit 0x0D 0x0A on a Windows system, it in fact doesn't?
>>
> 
> 
> You are absolutely right!
> 
> 
> 
>>Thanks,
>>
>>--Rob
>>
>>_______________________________________________
>>erlang-questions mailing list
>>
>>http://www.erlang.org/mailman/listinfo/erlang-questions
> 
> 



More information about the erlang-questions mailing list