Friday, 9 June 2017

Node reading file error

// Basic error checking for correct number of command line arguments
if (process.argv.length < 4 || process.argv.length > 4) {
    console.log("Usage:");
    console.log("node fileToConvertFrom fileToConvertTo");
    process.exit(-1);
}

// Capture commnad line arguments. Used get filenames for reading and writing from.
var convertFrom = process.argv[2];
var convertTo = process.argv[3];

var fs = require("fs");
var importFile = fs.readFileSync("./" + convertFrom).toString('utf-8');

//var arraySeperate = text.split("<ExternalTagHere>");
var arrayByLine = importFile.split("\n");

// without manually setting index 25 to what I am searching for, and in fact what it should be already be,
// this does not work and returns index "-1" which means not found. very fucking weird.
// comment out arrayByLine[25] = "" to not have this work
// something to do with cariage returns/tabs from stupid ass fucking windows?
arrayByLine[25] = "    <ExternalTagHere>";
var getIndex = arrayByLine.indexOf("    <ExternalTagHere>");
console.log(getIndex);

//console.log(arraySeperate[0]);

//arrayEcho(arrayByMachine);

function arrayCount(arr) {
    console.log("Array count: " + arr.length);
}

function arrayEcho(arr) {
    for (var i = 0; i <= arr.length; i++) {
            console.log(arr[i]);
    }
}

So the file I am importing/parsing is generated on Windows. I am parsing it on a Linux box with Node on the command line.

When I manually set the element, index 25, in the array indexOf method returns succesfully. However when I do not set it manually, indexOf can not find it. This is very odd to me, just looking at the text they look exactly the same. So, I am thinking it is Windows spacing versus Linux spacing? Or tabs? Sort of lost on this one.



via dottel

No comments:

Post a Comment