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

fs.open troubles

Started by Hydrotronics, 21 December 2015 - 01:55 PM
Hydrotronics #1
Posted 21 December 2015 - 02:55 PM
so, during the making of my OS, while waiting for things, i come across yet ANOTHER problem. This time i have no idea why it is happening. It won't create or write to the file "UserS".

This is the code which is executing it



local fileU = fs.open("UserS","a")
fileU.writeLine("test")
fileU.close()


Also, it won't write to the file when it had already been created. Also, it wont do it in write mode either. And no, no other program is using the file as the program that runs this is the only one on the computer. And no it doesn't give me an error :/
TheOddByte #2
Posted 21 December 2015 - 03:23 PM
This may sound stupid, but have you tried turning it off and on again? :P/>
Anyway, it may have happened that you've opened the file before and have forgotten to close it, because that can cause some errors.

I'd also suggest you try something like this

local file = fs.open( "Users", fs.exists( "Users" ) and "a" or "w" ) --# If the file exists then open it in append mode, otherwise open it in write mode
file.write( "test" )
file.close()
Hydrotronics #3
Posted 21 December 2015 - 03:36 PM
This may sound stupid, but have you tried turning it off and on again? :P/>

yes i have, that was actually one of the first things i did as that solves 90% of all technical problems :D/> but i shall try the alternative open thing u showed me :)/>

Edit: works! Ta! But one question, how does
and "a" or "w" 
work? Like why does it work like that?
Edited on 21 December 2015 - 02:41 PM
TheOddByte #4
Posted 21 December 2015 - 04:06 PM
Edit: works! Ta! But one question, how does
and "a" or "w" 
work? Like why does it work like that?
When opening a file in append mode it doesn't clear everything in the file, it adds to the existing data, and when using write mode it basically opens the file like it's empty so to say.
Hydrotronics #5
Posted 21 December 2015 - 04:08 PM
no i'm talking about like the and/or statements :P/>
KingofGamesYami #6
Posted 21 December 2015 - 04:14 PM
Ternary Operators are very fun. I use them way too much.
TheOddByte #7
Posted 21 December 2015 - 06:20 PM
KingOfGamesYami have already linked you the awesomeness, it's a neater way todo things and it can save you a lot of lines.
So let's say we had an if statement like this when we wanted to set a value to a variable

local number = 0

while true do

	--# Here's the if statement
	if number == 10 then
		number = 0
	else
		number = number + 1
	end

	print( number )
	sleep( 1 )
end
We could use ternary operators to crunch those 4 lines into 1 line

local number = 0

while true do

	number = number < 10 and number + 1 or 0 --# Here it checks if the number is smaller than then, if it is then add one to the number, if not, set it to 0

	print( number )
	sleep( 1 )

end
Edited on 21 December 2015 - 05:20 PM
Hydrotronics #8
Posted 21 December 2015 - 06:23 PM
okie! i get it now :3 thx
Dragon53535 #9
Posted 22 December 2015 - 12:45 AM
okie! i get it now :3 thx
I'm just gonna put this out there, other languages use the ? and : for theirs.

bool condition = (user == "Awesome") ? true : false;