I/O Woes

Monkey Forums/Monkey Programming/I/O Woes

zoqfotpik(Posted 2013) [#1]
I am testing a data loader which loads comma separated data like so:

106,0
1,1
3,0
1,1
1,0
1,1
10,0


The level load function is as follows. Note that the level name, "test2" is hardcoded for testing porpoises.

Function LoadLevel:Void(levelname:String)
	Local lines:String[]
	Local line:String
	Local values:String[]
	lines = LoadString("test2").Split("~n")
	 	For line = Eachin lines
		     values = line.Split(",")
		     Print "Key:" + values[0]
		     Print "Value:" + values[1]
		     Print "---"
		Next
End function


This gives an invalid array index for values[1].

The test2 file is located in the /data directory. Any thoughts? My guess is that the file is not being found correctly. How do we tell if a file exists?


muddy_shoes(Posted 2013) [#2]
This sort of thing is normally down to whitespace/end of line(~r~n) issues meaning that the string you think you have isn't actually what you get.

Print each line and see what it is. Do something like Print("*" + line + "*") so you can spot extra whitespace.


AdamRedwoods(Posted 2013) [#3]
you should probably check for a valid values length if you need two for each line:
	 	For line = Eachin lines
			values = line.Split(",")
	 		If values.Length<2 then Continue


also you'll need ".txt" after LoadString("test2.txt")


zoqfotpik(Posted 2013) [#4]
Are you saying that LoadString requires a .txt extension? That would certainly explain.

Where are things like this documented?


therevills(Posted 2013) [#5]
LoadString can load in different file types (defined in TEXT_FILES) and your file is actually called "test2.txt" not just "text2".


zoqfotpik(Posted 2013) [#6]
Thanks for your help.

That did indeed solve the issue. You know, I am really loath to criticize such an amazing product in any way-- but is there any place in the documentation where it states that files loaded by loadstring() should have the .txt extension?

In a great many of the issues people are experiencing, and certainly my own, it is not the product but the documentation that is falling short. The community here functions as an excellent support staff but I prefer to solve problems by reading the docs whenever possible and this is another instance where the docs were just insufficient, at least as far as anything I've seen.

Should I be reading the Monkey source itself?


Gerry Quinn(Posted 2013) [#7]
"Should I be reading the Monkey source itself?"

Sometimes that's the quickest way to find out something!


therevills(Posted 2013) [#8]
but is there any place in the documentation where it states that files loaded by loadstring() should have the .txt extension?


But it can load xml, txt and json by default, and more if you alter TEXT_FILES.

From the help:
Function LoadString : String ( path:String )
Loads a string from path.

So it loads in a string from a file path... your file is called test2.txt so you need to supply the full file name, the same if you had a file called test2.xml. If you didn't supply the extension how would Monkey know which file to load?


zoqfotpik(Posted 2013) [#9]
That would be rational...

the problem was, the file was generated by me, with no extension and therefore no way for my system to know that it was a .txt file. I generated it using a Blitzmax program and saving it as "text2" with no extension.

So either blitzmax was putting an extension on the file or loadstring would not load files without a .txt extension. It works fine with the .txt extension, by the way.

Most peculiar.

It's also just possible that I semi-intentionally appended .txt to the filename in my bmax program, in which case I will be duly mortified.


therevills(Posted 2013) [#10]
Ahhh that makes more sense, I didn't know that the file you generated was just "text2" I thought it was just a typo.

From my understanding Monkey scans your data folder and checks the TEXT_FILES filter and transfers the extensions found to the build folder, so yes Monkey requires an extension, unless your TEXT_FILES is just *.* (I havent tested this).


zoqfotpik(Posted 2013) [#11]
I checked and it is indeed a naked filename with no extension.

What do you think about the idea of a comprehensive set of docs for monkey? The blitzmax one on wikibooks is one of the best documentations for a programming language that I ever saw.

I wasn't aware of the move filter. That makes a great deal of sense but is extremely obscure. Is there a document on the build system that I could read?