0

我有一个脚本,我正在玩弄它来格式化针对特定场景的所有生成的排列,这样我就可以为下一部分创建一个字符串数组。无论出于何种原因,当我输出到我的“输出”文件时,文件停止在 ' ["1-12-17-"], ' 而它应该在 ' ["25-24-23-"], ' 结束时控制台输出以正确的字符串结束,但文本文件停止很短。

这是代码:

    var fs = require('fs');

    // 13800
    var input = [ [1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 2, 6], [1, 2, 7], [1, 2, 8], [1, 2, 9], [1, 2, 10], [1, 2, 11], [1, 2, 12], [1, 2, 13], [1, 2, 14], [1, 2, 15], [1, 2, 16], [1, 2, 17], [1, 2, 18], [1, 2, 19], [1, 2, 20], [1, 2, 21], [1, 2, 22], [1, 2, 23], [1, 2, 24], [1, 2, 25], [1, 3, 2], [1, 3, 4], [1, 3, 5], [1, 3, 6], [1, 3, 7], [1, 3, 8], [1, 3, 9], [1, 3, 10], [1, 3, 11], [1, 3, 12], [1, 3, 13], [1, 3, 14], [1, 3, 15], [1, 3, 16], [1, 3, 17], [1, 3, 18], [1, 3, 19], [1, 3, 20], [1, 3, 21], [1, 3, 22], [1, 3, 23], [1, 3, 24], [1, 3, 25], [1, 4, 2], [1, 4, 3], [1, 4, 5], [1, 4, 6], [1, 4, 7], [1, 4, 8], [1, 4, 9], [1, 4, 10], [1, 4, 11], [1, 4, 12], [1, 4, 13], [1, 4, 14], [1, 4, 15], [1, 4, 16], [1, 4, 17], [1, 4, 18], [1, 4, 19], [1, 4, 20], [1, 4, 21], [1, 4, 22], [1, 4, 23], [1, 4, 24], [1, 4, 25], [1, 5, 2], [1, 5, 3], [1, 5, 4], [1, 5, 6], [1, 5, 7], [1, 5, 8], [1, 5, 9], [1, 5, 10], [1, 5, 11], [1, 5, 12], [1, 5, 13], [1, 5, 14], [1, 5, 15], [1, 5, 16], [1, 5, 17], [1, 5, 18], [1, 5, 19], [1, 5, 20], [1, 5, 21], [1, 5, 22], [1, 5, 23], [1, 5, 24], [1, 5, 25], [1, 6, 2], [1, 6, 3], [1, 6, 4], [1, 6, 5], [1, 6, 7], [1, 6, 8], [1, 6, 9], [1, 6, 10], [1, 6, 11], [1, 6, 12], [1, 6, 13], [1, 6, 14], [1, 6, 15], [1, 6, 16], [1, 6, 17], [1, 6, 18], [1, 6, 19], [1, 6, 20], [1, 6, 21], [1, 6, 22], [1, 6, 23], [1, 6, 24], [1, 6, 25], [1, 7, 2], [1, 7, 3], [1, 7, 4], [1, 7, 5], [1, 7, 6], [1, 7, 8], [1, 7, 9], [1, 7, 10], [1, 7, 11], [1, 7, 12], [1, 7, 13], [1, 7, 14], [1, 7, 15], [1, 7, 16], [1, 7, 17], [1, 7, 18], [1, 7, 19], [1, 7, 20], [1, 7, 21], [1, 7, 22], [1, 7, 23], [1, 7, 24], [1, 7, 25], [1, 8, 2], [1, 8, 3], [1, 8, 4], [1, 8, 5], [1, 8, 6], [1, 8, 7], [1, 8, 9], [1, 8, 10], [1, 8, 11], [1, 8, 12], [1, 8, 13], [1, 8, 14], [1, 8, 15], [1, 8, 16], [1, 8, 17], [1, 8, 18], [1, 8, 19], [1, 8, 20], [1, 8, 21], [1, 8, 22], [1, 8, 23], [1, 8, 24], [1, 8, 25], [1, 9, 2], [1, 9, 3], [1, 9, 4], [1, 9, 5], [1, 9, 6], [1, 9, 7], [1, 9, 8], [1, 9, 10], [1, 9, 11], [1, 9, 12], [1, 9, 13], [1, 9, 14], [1, 9, 15], [1, 9, 16], [1, 9, 17], [1, 9, 18], [1, 9, 19], [1, 9, 20], [1, 9, 21], [1, 9, 22], [1, 9, 23], [1, 9, 24], [1, 9, 25], [1, 10, 2], [1, 10, 3], [1, 10, 4], [1, 10, 5], [1, 10, 6], [1, 10, 7], [1, 10, 8], [1, 10, 9], [1, 10, 11], [1, 10, 12], [1, 10, 13], [1, 10, 14], [1, 10, 15], [1, 10, 16], [1, 10, 17], [1, 10, 18], [1, 10, 19], [1, 10, 20], [1, 10, 21], [1, 10, 22], [1, 10, 23], [1, 10, 24], [1, 10, 25], [1, 11, 2], [1, 11, 3], [1, 11, 4], [1, 11, 5], [1, 11, 6], [1, 11, 7], [1, 11, 8], [1, 11, 9], [1, 11, 10], [1, 11, 12], [1, 11, 13], [1, 11, 14], [1, 11, 15], [1, 11, 16], [1, 11, 17], [1, 11, 18], [1, 11, 19], [1, 11, 20], [1, 11, 21], [1, 11, 22], [1, 11, 23], [1, 11, 24], [1, 11, 25], [1, 12, 2], [1, 12, 3], [1, 12, 4], [1, 12, 5], [1, 12, 6], [1, 12, 7], [1, 12, 8], [1, 12, 9], [1, 12, 10], [1, 12, 11], [1, 12, 13], [1, 12, 14], [1, 12, 15], [1, 12, 16], [1, 12, 17], [1, 12, 18], [1, 12, 19], [1, 12, 20], [1, 12, 21], [1, 12, 22], [1, 12, 23], [1, 12, 24], [1, 12, 25], [1, 13, 2], [1, 13, 3], [1, 13, 4], [1, 13, 5], [1, 13, 6], [1, 13, 7], [1, 13, 8], [1, 13, 9], [1, 13, 10], [1, 13, 11], [1, 13, 12], [1, 13, 14], [1, 13, 15], [1, 13, 16], [1, 13, 17], [1, 13, 18], [1, 13, 19], [1, 13, 20], [1, 13, 21], [1, 13, 22], [1, 13, 23], [1, 13, 24], [1, 13, 25], [1, 14, 2], [1, 14, 3], [1, 14, 4], [1, 14, 5], [1, 14, 6], [1, 14, 7], [1, 14, 8], [1, 14, 9], [1, 14, 10], [1, 14, 11], [1, 14, 12], [1, 14, 13], [1, 14, 15], [1, 14, 16], [1, 14, 17], [1, 14, 18], [1, 14, 19], [1, 14, 20], [1, 14, 21], [1, 14, 22], [1, 14, 23], [1, 14, 24], [1, 14, 25], [1, 15, 2], [1, 15, 3], [1, 15, 4], [1, 15, 5], [1, 15, 6], [1, 15, 7], [1, 15, 8], [1, 15, 9], [1, 15, 10], [1, 15, 11], [1, 15, 12], [1, 15, 13], [1, 15, 14], [1, 15, 16], [1, 15, 17], [1, 15, 18], [1, 15, 19], [1, 15, 20], [1, 15, 21], [1, 15, 22], [1, 15, 23], [1, 15, 24], [1, 15, 25], [1, 16, 2], [1, 16, 3], [1, 16, 4], [1, 16, 5], [1, 16, 6], [1, 16, 7], [1, 16, 8], [1, 16, 9], [1, 16, 10], [1, 16, 11], [1, 16, 12], [1, 16, 13], [1, 16, 14], [1, 16, 15], [1, 16, 17], [1, 16, 18], [1, 16, 19], [1, 16, 20], [1, 16, 21], [1, 16, 22], [1, 16, 23], [1, 16, 24], [1, 16, 25], [1, 17, 2], [1, 17, 3], [1, 17, 4], [1, 17, 5], [1, 17, 6], [1, 17, 7], [1, 17, 8], [1, 17, 9], [1, 17, 10], [1, 17, 11], [1, 17, 12], [1, 17, 13], [1, 17, 14], [1, 17, 15], [1, 17, 16], [1, 17, 18], [1, 17, 19], [1, 17, 20], [1, 17, 21], [1, 17, 22], [1, 17, 23], [1, 17, 24], [1, 17, 25], [1, 18, 2], [1, 18, 3], [1, 18, 4], [1, 18, 5], [1, 18, 6], [1, 18, 7], [1, 18, 8], [1, 18, 9], [1, 18, 10], [1, 18, 11], [1, 18, 12], [1, 18, 13], [1, 18, 14], [1, 18, 15], [1, 18, 16], [1, 18, 17], [1, 18, 19], [1, 18, 20], [1, 18, 21], [1, 18, 22], [1, 18, 23], [1, 18, 24], [1, 18, 25], [1, 19, 2], [1, 19, 3], [1, 19, 4], [1, 19, 5], [1, 19, 6], [1, 19, 7], [1, 19, 8], [1, 19, 9], [1, 19, 10], [1, 19, 11], [1, 19, 12], [1, 19, 13], [1, 19, 14], [1, 19, 15], [1, 19, 16], [1, 19, 17], [1, 19, 18], [1, 19, 20], [1, 19, 21], [1, 19, 22], [1, 19, 23], [1, 19, 24], [1, 19, 25], [1, 20, 2], [1, 20, 3], [1, 20, 4], [1, 20, 5], [1, 20, 6], [1, 20, 7], [1, 20, 8], [1, 20, 9], [1, 20, 10], [1, 20, 11], [1, 20, 12], [1, 20, 13], [1, 20, 14], [1, 20, 15], [1, 20, 16], [1, 20, 17], [1, 20, 18], [1, 20, 19], [1, 20, 21], [1, 20, 22], [1, 20, 23], [1, 20, 24], [1, 20, 25], [1, 21, 2], [1, 21, 3], [1, 21, 4], [1, 21, 5], [1, 21, 6], [1, 21, 7], [1, 21, 8], [1, 21, 9], [1, 21, 10], [1, 21, 11], [1, 21, 12], [1, 21, 13], [1, 21, 14], [1, 21, 15], [1, 21, 16], [1, 21, 17], [1, 21, 18], [1, 21, 19], [1, 21, 20], [1, 21, 22], [1, 21, 23], [1, 21, 24], [1, 21, 25], [1, 22, 2], [1, 22, 3], [1, 22, 4], [1, 22, 5], [1, 22, 6], [1, 22, 7], [1, 22, 8], [1, 22, 9], [1, 22, 10], [1, 22, 11], [1, 22, 12], [1, 22, 13], [1, 22, 14], [1, 22, 15], [1, 22, 16], [1, 22, 17], [1, 22, 18], [1, 22, 19], [1, 22, 20], [1, 22, 21], [1, 22, 23], [1, 22, 24], [1, 22, 25], [1, 23, 2], [1, 23, 3], [1, 23, 4], [1, 23, 5], [1, 23, 6], [1, 23, 7], [1, 23, 8], [1, 23, 9], [1, 23, 10], [1, 23, 11], [1, 23, 12], [1, 23, 13], [1, 23, 14], [1, 23, 15], [1, 23, 16], [1, 23, 17], [1, 23, 18], [1, 23, 19], [1, 23, 20], [1, 23, 21], [1, 23, 22], [1, 23, 24], [1, 23, 25], [1, 24, 2], [1, 24, 3], [1, 24, 4], [1, 24, 5], [1, 24, 6], [1, 24, 7], [1, 24, 8], [1, 24, 9], [1, 24, 10], [1, 24, 11], [1, 24, 12], [1, 24, 13], [1, 24, 14], [1, 24, 15], [1, 24, 16], [1, 24, 17], [1, 24, 18], [1, 24, 19], [1, 24, 20], [1, 24, 21], [1, 24, 22], [1, 24, 23], [1, 24, 25], [1, 25, 2], [1, 25, 3], [1, 25, 4], [1, 25, 5], [1, 25, 6], [1, 25, 7], [1, 25, 8], [1, 25, 9], [1, 25, 10], [1, 25, 11], [1, 25, 12], [1, 25, 13], [1, 25, 14], [1, 25, 15], [1, 25, 16], [1, 25, 17], [1, 25, 18], [1, 25, 19], [1, 25, 20], [1, 25, 21], [1, 25, 22], [1, 25, 23], [1, 25, 24] ]; 

    // I had to dramatically shorten the data

    var temp = [];

    //console.log(input.length);

    for (var x = 0; x < input.length; x++) {
        temp[x] = "[\"" + input[x][0] + "-" + input[x][1] + "-" + input[x][2] + "-\"], ";

        if (true) {
            fs.appendFile("output", temp[x], function (err) {});
            console.log("HERE - " + temp[x]);
        }
    }

有任何想法吗?输出是否表明 node.js 的限制?或者可能是文件行限制?

任何帮助将不胜感激。

4

1 回答 1

3

fs.appendFile 是异步的,因此对文件的写入可能不会按照您期望的顺序发生。您可以使用 fs.appendFileSync 而不是 fs.appendFile 轻松解决问题。或者您可以更改您的代码以以正确的方式使用异步方法:

var fs = require('fs');

var input = [];
for (var i = 1; i < 25; i++)
    for (var j = 1; j < 25; j++)
        for (var k = 1; k < 25; k++)
            input.push([i, j, k]);

var temp = [];

/* Synchronous method */
for (var x = 0; x < input.length; x++) {
    temp[x] = makeStr(input[x]);

    fs.appendFileSync("output.txt", temp[x]);
    console.log("HERE - " + temp[x]);
}

/* Asynchronous method */
var i = 0,
    callback = function (err) {
        i++;
        if (!err && i < input.length) {
            temp.push(makeStr(input[i]));
            fs.appendFile("output.txt", temp[i], callback);
        }
    };

temp.push(makeStr(input[i]));
fs.appendFile("output.txt", temp[i], callback);

function makeStr(item) {
    return '["' + item[0] + '-' + item[1] + '-' + item[2] + '"]';
}
于 2013-06-26T19:06:01.707 回答