openfile\writefile help!

Blitz3D Forums/Blitz3D Beginners Area/openfile\writefile help!

blade007(Posted 2007) [#1]
ahh this isnt working, it keeps adding zero to the file!some 1 plz help!!
Function save_settings()
	If FileType("settings.txt") <> 1 Then WriteFile("settings.txt")
	file=OpenFile("settings.txt")
	WriteLine(file,mousebutton1_assign)
	WriteLine(file,mousebutton2_assign)
	WriteLine(file,mousebutton3_assign)
	CloseFile(file)
End Function

Function load_settings()
	If FileType("settings.txt")
		file=OpenFile("settings.txt")
		mousebutton1_assign=ReadLine(mousebutton1_assign)
		mousebutton2_assign=ReadLine(mousebutton2_assign)
		mousebutton3_assign=ReadLine(mousebutton3_assign)	
		CloseFile(file)
	EndIf
End Function



Sledge(Posted 2007) [#2]
Coupla' problems I can immediately spot:

If FileType("settings.txt") <> 1 Then WriteFile("settings.txt")
You're not assigning WriteFile to anything, and if you intend to overwrite the file contents then you should always be accessing it with WriteFile rather than trying to create it then OpenFile it. OpenFile will tack the new data on the end (erm, or at least allows you to). Also what if "settings.txt" is a directory for some bizarre unforeseen reason?

mousebutton1_assign=ReadLine(mousebutton1_assign)
Should be a stream in the parenthesis there.

Is the any reason why you're not using Read/WriteInt()?

EDIT: Also check that the variables you want to save are global otherwise they WILL all be freshly initialised local zeroes within the functions.

Anyway, try...


Fuller example showing data retrieval:

Note that if you open settings.txt in Notepad you'll be able to scroll down to a blank line because WriteLine() adds a line-feed and carriage-return to the data (thus making it a line), which is why you might want to consider Read/WriteInt(). You might even want to use Read/WriteByte() if the numbers are going to fall within the range 0-255.


blade007(Posted 2007) [#3]
lol thank's dude! i'll keep in mind those error messages...