{"id":937,"date":"2022-08-30T15:09:40","date_gmt":"2022-08-30T15:09:40","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/node-js-breaks-my-output-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:09:40","modified_gmt":"2022-08-30T15:09:40","slug":"node-js-breaks-my-output-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/node-js-breaks-my-output-collection-of-common-programming-errors\/","title":{"rendered":"Node.js breaks my output-Collection of common programming errors"},"content":{"rendered":"<p>I have basically something such as :<\/p>\n<pre><code>var escape = function ( x ) {\n    if ( ! x ) return '\\\\N';\n    return '\"' + x.replace( \/\\r\\n\/g, '\\n' ) + '\"';\n};\n\narray.forEach( function ( item ) {\n    process.stdout.write( 'foo:' + [ a, b, d ].map( escape ).join( '\\t' ) + '\\n' );\n    item.child.forEach( function ( sub ) {\n        process.stdout.write( 'bar:' + [ e, f, g ].map( escape ).join( '\\t' ) + '\\n' );\n    } );\n} );\n<\/code><\/pre>\n<p>However, the output file contains, at apparently random locations, strings such as :<\/p>\n<pre><code>bar:\\N        \"1981258\"       \"Can't Get N^@^@^@^@^@^@^@^@^@^@Her Bones In Thefoo:\"1981259\" \"164264\"\nbar:\\N        ^@ Left\"        \\N      \\N\nbar:^@^@^@    \\N\n<\/code><\/pre>\n<p>It does not make sense for me, since the program should not be able to print anything without wrapping it into double quotes.<\/p>\n<p>I think it&#8217;s a Node.js bug, due to this line, but I just want to know if there is a workaround.<\/p>\n<pre><code>b^@^@^@^@^@^@^@^@^@x 1 root root 1,5G 17 d\\303\\251c.  19:14 \/D\/Release.cs\n<\/code><\/pre>\n<ol>\n<li>\n<p>It was probably an issue related to the filesystem were the file was wrote (vmhgfs). The second generated file did not have this garbage.<\/p>\n<\/li>\n<li>\n<p>You don&#8217;t have to escape crlf sequences.<\/p>\n<pre><code>$ node\n&gt; var crlf = '\\n';\nundefined\n&gt; process.stdout.write( 'foo:' + crlf + 'bar:');\nfoo:\nbar:true\n&gt; process.stdout.write( 'foo:' + crlf + 'bar:' + crlf + '\\\\n' + 'foobar?');\nfoo:\nbar:\n\\nfoobar?true\n&gt;\n<\/code><\/pre>\n<p>(The &#8220;true&#8221; 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&#8217;t know what a, b, d, e, f, g,&#8230; refer to).<\/p>\n<p>Also, depending on your input (and seeing your output), it may make more sense for you to use Buffers instead of raw strings.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:00:54. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have basically something such as : var escape = function ( x ) { if ( ! x ) return &#8216;\\\\N&#8217;; return &#8216;&#8221;&#8216; + x.replace( \/\\r\\n\/g, &#8216;\\n&#8217; ) + &#8216;&#8221;&#8216;; }; array.forEach( function ( item ) { process.stdout.write( &#8216;foo:&#8217; + [ a, b, d ].map( escape ).join( &#8216;\\t&#8217; ) + &#8216;\\n&#8217; ); item.child.forEach( function [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-937","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/937","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=937"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/937\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=937"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=937"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=937"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}