DWQA QuestionsCategory: ProgramThe file nodejs wrote cannot wrap
Inexplicable Xiaosheng asked 1 month ago

I used casperjs to do a demo to test the loading speed of web pages, and then wanted to print the results on a TXT through fs.write. I added the newline character ‘n’, but the printed things still don’t newline. One of the code sections is like this:

var timeouttxt = '';
function timeOutTxt() {
    if (timend > 800) {
       timeouttxt = 'Page Times is ' + timend + ' is timeout 800\n';
    } else {
       timeouttxt = 'Page Times is ' + timend + '\n';
    }
}

txtData += 'the links length: ' + links.length + '\n' 
        + 'Page url is ' + this.getCurrentUrl() + '\n'
        + 'Page title is ' + this.getTitle() + ' index: ' + index + '\n'
        + timeouttxt
        + '-----------------------------------------------------\n';

fs.write(file, txtData, function(err) {
    if (err) return err;
});

The input TXT is similar to this:

the links length: 23Page url is  http://10.1.1.120/CRM/Page  Title is health and safety management system index: 0 the links length: 23page URL ishttp://10.1.1.120/CRM/indexPage  Title is health safety management system index: 1 the links length: 23page URL is http://10.1.1.120/ CRM / updatepasswordpage title is Hong Kong Health and safety management system index: 2 the links length: 23page URL is http://10.1.1.120/approve/Page  Title is health safety management system index: 3 the links length: 23page URL is http://10.1.1.120/notice/Page  title is  index:  4-----------------------------------------------------the links length: 23Page url is  http://10.1.1.120/CRM/salesopportunities/indexPage  Title is health safety management system index: 5 the links length: 23page URL ishttp://10.1.1.120/CRM/contact/homepagePage  Title is health safety management system index: 6

What is the reason? How to change? thank you.

kikong replied 1 month ago

\r\n

kikong replied 1 month ago

Thank you very much

1 Answers
liujians answered 1 month ago
\R enter the line feed
windows \r\n linux \n mac \r
In the past, typewriters used to have carriage return and line feed at the end of a line. Windows inherits this, but it's a waste to mark the end of a line with two characters, so UNIX and Mac only use one character, while Linux inherits UNIX.
Inexplicable Xiaosheng replied 1 month ago

I tried it. It’s really the problem. Thank you

liujians replied 1 month ago

you are welcome

zhaoxing replied 1 month ago

You have to use RN under Linux