Node.js breaks my output-Collection of common programming errors

I have basically something such as :

var escape = function ( x ) {
    if ( ! x ) return '\\N';
    return '"' + x.replace( /\r\n/g, '\n' ) + '"';
};

array.forEach( function ( item ) {
    process.stdout.write( 'foo:' + [ a, b, d ].map( escape ).join( '\t' ) + '\n' );
    item.child.forEach( function ( sub ) {
        process.stdout.write( 'bar:' + [ e, f, g ].map( escape ).join( '\t' ) + '\n' );
    } );
} );

However, the output file contains, at apparently random locations, strings such as :

bar:\N        "1981258"       "Can't Get N^@^@^@^@^@^@^@^@^@^@Her Bones In Thefoo:"1981259" "164264"
bar:\N        ^@ Left"        \N      \N
bar:^@^@^@    \N

It does not make sense for me, since the program should not be able to print anything without wrapping it into double quotes.

I think it’s a Node.js bug, due to this line, but I just want to know if there is a workaround.

b^@^@^@^@^@^@^@^@^@x 1 root root 1,5G 17 d\303\251c.  19:14 /D/Release.cs
  1. It was probably an issue related to the filesystem were the file was wrote (vmhgfs). The second generated file did not have this garbage.

  2. You don’t have to escape crlf sequences.

    $ node
    > var crlf = '\n';
    undefined
    > process.stdout.write( 'foo:' + crlf + 'bar:');
    foo:
    bar:true
    > process.stdout.write( 'foo:' + crlf + 'bar:' + crlf + '\\n' + 'foobar?');
    foo:
    bar:
    \nfoobar?true
    >
    

    (The “true” at the end of both line is the return value from process.stdout.write in the REPL) That said, could you post a gist with a sample input and more code (I don’t know what a, b, d, e, f, g,… refer to).

    Also, depending on your input (and seeing your output), it may make more sense for you to use Buffers instead of raw strings.

Originally posted 2013-11-09 23:00:54.