0

I keep banging my head against the wall because of tons of different errors. This is what the code i try to use :

fs.readFile("balance.txt", function (err, data) //At the beginning of the script (checked, it works)
{
  if (err) throw err;
  balance=JSON.parse(data);;
});
fs.readFile("pick.txt", function (err, data)
{
  if (err) throw err;
  pick=JSON.parse(data);;
});

/*....
.... balance and pick are modified
....*/

if (shutdown)
{
        fs.writeFile("balance2.txt", JSON.stringify(balance));
        fs.writeFile("pick2.txt", JSON.stringify(pick));
        process.exit(0);
}

At the end of the script, the files have not been modified the slightest. I then found out on this site that the files were being opened 2 times simultaneously, or something like that, so i tried this :

var balance, pick;
var stream = fs.createReadStream("balance.txt");
stream.on("readable", function()
{
    balance = JSON.parse(stream.read());
});
var stream2 = fs.createReadStream("pick.txt");
stream2.on("readable", function()
{
    pick = JSON.parse(stream2.read());
});

/****
****/

fs.unlink("pick.txt");
            fs.unlink("balance.txt");
            var stream = fs.createWriteStream("balance.txt", {flags: 'w'});
            var stream2 = fs.createWriteStream("pick.txt", {flags: 'w'});
            stream.write(JSON.stringify(balance));
            stream2.write(JSON.stringify(pick));
            process.exit(0);

But, this time, both files are empty... I know i should catch errors, but i just don't see where the problem is. I don't mind storing the 2 objects in the same file, if that can helps. Besides that, I never did any javascript in my life before yesterday, so, please give me a simple explanation if you know what failed here.

tshepang
  • 12,111
  • 21
  • 91
  • 136
pie3636
  • 795
  • 17
  • 31

1 Answers1

1

What I think you want to do is use readFileSync and not use readFile to read your files since you need them to be read before doing anything else in your program (http://nodejs.org/api/fs.html#fs_fs_readfilesync_filename_options).

This will make sure you have read both the files before you execute any of the rest of your code.

Make your like code do this:

try
{
    balance = JSON.parse(fs.readFileSync("balance.txt"));
    pick = JSON.parse(fs.readFileSync("pick.txt"));
}
catch(err)
{    throw err;    }

I think you will get the functionality you are looking for by doing this.

Note, you will not be able to check for an error in the same way you can with readFile. Instead you will need to wrap each call in a try catch or use existsSync before each operation to make sure you aren't trying to read a file that doesn't exist.

How to capture no file for fs.readFileSync()?

Furthermore, you have the same problem on the writes. You are kicking off async writes and then immediately calling process.exit(0). A better way to do this would be to either write them sequentially asynchronously and then exit or to write them sequentially synchronously then exit.

Async option:

if (shutdown)
{
    fs.writeFile("balance2.txt", JSON.stringify(balance), function(err){
        fs.writeFile("pick2.txt", JSON.stringify(pick), function(err){
            process.exit(0);
        });
    });
}

Sync option:

if (shutdown)
{
    fs.writeFileSync("balance2.txt", JSON.stringify(balance));
    fs.writeFileSync("pick2.txt", JSON.stringify(pick));
    process.exit(0);
}
Community
  • 1
  • 1
Brian
  • 3,264
  • 4
  • 30
  • 43
  • I tried the code you gave me, and the reading of the file works as well as earlier... Then i modified "balance" only, and the file balance.txt was empty at the end of the script, while pick.txt was unchange... I'm at a loss – pie3636 Aug 20 '13 at 17:42
  • Oh, I see it now. You also are calling process.exit(0) after you call async write calls too. I will update my answer to show you how to do that one as well. – Brian Aug 20 '13 at 17:52
  • Let me know how it turns out, I think that should fix your problems – Brian Aug 20 '13 at 18:11
  • It's okay, someone just told me that the process.exit(0) killed the program before all had been writed. I solved it by using write.end() and write.on("finish"). Thanks for your fast anwers ! – pie3636 Aug 20 '13 at 18:32