This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Strikingwolf's profile picture

Trouble with the io API

Started by Strikingwolf, 02 December 2014 - 11:42 PM
Strikingwolf #1
Posted 03 December 2014 - 12:42 AM
Here is my code. I am trying to make a login system (the passwords and such will be encrypted don't worry). I was just testing to see that all the users where saved, but when this prints it only prints the last user. Does anyone have any idea why it did that because I cannot figure it out?
Dog #2
Posted 03 December 2014 - 01:26 AM
It looks like the problem is that you're opening your files for writing when you should probably be opening them for appending. When you open a file for writing ("w") the entire file is overwritten.

Try something like this - it will open for writing if the file doesn't exist and open for appending if the file already exists…

users = io.open(usersDir, fs.exists(usersDir) and "a" or "w")
pass = io.open(passDir, fs.exists(passDir) and "a" or "w")
Edited on 03 December 2014 - 12:29 AM
Strikingwolf #3
Posted 03 December 2014 - 01:28 AM
Can I open for appending without first opening for writing?

It looks like the problem is that you're opening your files for writing when you should probably be opening them for appending. When you open a file for writing ("w") the entire file is overwritten.

Try something like this - it will open for writing if the file doesn't exist and open for appending if the file already exists…

users = io.open(usersDir, fs.exists(usersDir) and "a" or "w")
pass = io.open(passDir, fs.exists(passDir) and "a" or "w")
I had just asked if that would work for writing, but I was waiting for a moderator to approve it :P/>/> . Thanks for the help.

Also, do you know how I could get each line without the newline, write now it prints with the newline, but when I'm doing the login function I would like it to not have the newline
Bomb Bloke #4
Posted 03 December 2014 - 02:00 AM
You never want to open an empty file in append mode, if that's what you're asking. "Append" means "add on to the end". If you've never first written something to your file, then what are you going to append to?

Certain versions of ComputerCraft will, if you attempt to open a non-existent file in "append" mode, auto-open them in "write" mode instead. Other versions won't open the file at all. The code Dog's suggesting solves that particular problem by manually checking whether the file exists and specifically selecting the appropriate mode.

I'm not sure what you mean by the "newline" stuff. I can tell you the "print" function always performs a line break after whatever it outputs, whereas the "write" function does not.
Dog #5
Posted 03 December 2014 - 02:01 AM
Yes, you can open for appending without first opening for writing :)/>

As for writing each line, you could try replacing write with writeLine, but you may have to change your syntax (e.g. users.writeLine(…) instead of users:writeLine(…\n) - note the dot instead of the colon).

EDIT: :ph34r:/> 'd by BB

I like BB's suggestion of using print instead of write - keeps it simple.
Edited on 03 December 2014 - 01:02 AM
Strikingwolf #6
Posted 03 December 2014 - 02:08 AM
I was asking how I could get it without the newline :P/>, but I thought the print function didn't add a newline. Armed with that info I think I'm ready to start making this system.