Search code examples
node.jsreadfile

Incorrect reading lines node.js file


Hello I have a txt file to read with node.js (the contents of the txt file is below), when it reads the file with the javascript code below, the reading is performed correctly only on the first line of the file while the second it is not read correctly! How can I solve this?

File.TXT:

2;49.805;19.3386
3;49.805;19.3386

Node.js Code:

fs.readFile(tempfilename, function (err, data) {
            if (err) {
                ManageError.SendError("error: " + err);
            } else {
                var array = data.toString().split("\n");
                for (i in array) {
                    var campi = array[i].split(';');
                    console.log(array[i]);
                    TIMESET(campi[0], campi[1], campi[2], "In");
                }
            }
        });

Solution

  • If you're working off a windows machine, windows .txt files have \r\n instead of just \n. This is evident if you add console.log(campi) and look at the second entry. Perhaps that's the issue?

    Otherwise on my system the code runs fine.