One solution is to write to the file as you do the calculations. I have modified your example (untested) to show how this works.
directory = getDirectory("Choose a Directory");
resultFilename = directory + Dialog.getString() + ".csv";
f = File.open(resultFilename);
A = newArray(nResults() - 1);
B = newArray(nResults() - 1);
// no C array is made so:
C = newArray(nResults() - 1);
D = getResult("B", nResults() - 1);
for (i = 0; i < nResults() - 2; i++) {
A[i] = getResult("A", i);
B[i] = getResult("B", i);
C[i] = A[i] - D;
// should the line above should be C[i] = B[i] - D;
print(f, d2s(A[i],6) + " \t" + d2s(B[i],6) + " \t" + d2s(C[i],6));
}
File.close(f);
Note that you don't need to make the arrays at all and can just write to the file (again this is untested):
directory = getDirectory("Choose a Directory");
resultFilename = directory + Dialog.getString() + ".csv";
f = File.open(resultFilename);
D = getResult("B", nResults() - 1);
for (i = 0; i < nResults() - 2; i++) {
ai = getResult("A", i);
bi = getResult("B", i);
ci = ai - D;
// should the line above should be ci = bi - D;
print(f, d2s(ai,6) + " \t" + d2s(bi,6) + " \t" + d2s(ci,6));
}
File.close(f);
I have used " \t" (tab character) as a separator not comma.