1

My code is

for (let indexM = 0; indexM < 0.5; indexM += 0.01) {
  //C
  for (let indexC = -10; indexC < 10; indexC += 0.05) {
    //X
    for (let indexX = 0; indexX < X.length; indexX++) {
      //Y = M * X[i] + C
      PredictedY = indexM * X[indexX] + indexC;
      Error += (PredictedY - Y[indexX]) * (PredictedY - Y[indexX]);
    }
    newMeanSquareError = Error / X.length;

    let M = indexM.toFixed(2);
    let C = indexC.toFixed(2);
    let MSE = newMeanSquareError.toFixed(2);

    // var data = `M slope: ${M} ` + `  Y-intercept: ${C} ` + `  MSE: ${MSE} ` + "\r\n";

    fs.writeFile(
      "HouseSales.txt",
      `M Slope : ${M}  C Intercept : ${C} MSE : ${MSE}`,
      (err) => {
        // In case of a error throw err.
        if (err) throw err;
      }
    );

    if (least > newMeanSquareError) {
      least = newMeanSquareError;
      newC = indexC;
      newM = indexM;
    }
    Error = 0;
  }
}

I am getting this error


[Error: EMFILE: too many open files, open 'F:\Semester 5\Introductiob to AI\Lab1\HouseSales.txt'] {
  errno: -4066,
  code: 'EMFILE',
  syscall: 'open',
  path: 'F:\\Semester 5\\Introductiob to AI\\Lab1\\HouseSales.txt'
}

Is there any way where I can write to a file in a very big loop in JavaScript ? I have tried fs but I am not getting a solution. I think fs cannot help when it comes to writing to a file 1000s of times or more.

2
  • You should consider a more traditional approach. Namely, open the file outside the loop, only write bytes to the file inside the loop, and close the file after the loop. Should work just fine. fsOpen, fsWrite, fsClose Commented Oct 1, 2020 at 20:47
  • Yes. Fs has multiple different ways of doing the same thing (in this case writing to a file), so you can choose the one which suits you best. This one fails because it's asynchronous. It writes to the file in background while your loop continues. However your loop is much faster than the file write so you end up re-opening the file before the last write has finished. Try writeFileSync. Commented Oct 1, 2020 at 20:59

1 Answer 1

3

you should create a stream before the loop and write to it in he loop:

var out = fs.createWriteStream("HouseSales.txt", { flags : 'a' });

a flag is for appending, it will create the file if it doesnt exist. and use this to write:

out.write(`M Slope : ${M}  C Intercept : ${C} MSE : ${MSE}`, 'utf-8');

finally (after the loop) close the stream:

out.end();
Sign up to request clarification or add additional context in comments.

1 Comment

should there be a newline in the string?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.