|
Post by Voodoo on Feb 26, 2004 23:03:42 GMT -5
hey all, i just started using basic4gl and i can't seem to figure out how to read info from files. the problem is thus: i have a .raw file which contains one header line and 4 lines of 3 vertex triplets. i can't figure out how to read this information into an array or something. thanx, cya.
|
|
|
Post by Tom Mulgrew on Mar 3, 2004 0:21:38 GMT -5
Hi, sorry about the delay. There's a File I/O section in the "Programmer's Guide", available from the help menu, which lists the basic functions available. You'll want to open the file with "OpenFileRead" and then read out the information. Which functions you use to read it depends on the format of the file. If the data is stored in binary format, then ReadByte, ReadWord, ReadInt, ReadFloat or ReadDouble (depending on the format and size of the data). If it's stored in text format, there's a function called ReadText(), which reads a string of continuous text characters, separated by whitespace. (Unfortunately I forgot to put it in the documentation, but the documentation will read: ReadText(file, skipEOL) skips over whitespace (spaces, tabs e.t.c) until it finds some text. It then returns all the consecutive text at that point until a whitespace character has been reached, as a string. SkipEOL is a boolean (true/false) parameter. If it is true, then ReadText will skip over any end-of-line characters it finds in the file. If false, it will stop at the end-of-line and return a blank string.You'll need the Val function (converts strings to numbers) also. A very simple file reading example follows. dim file, text$ ' File handle, temp string storage
' Open file file = OpenFileRead ("files\test.txt") if FileError () <> "" then print FileError () end endif
' Read from file while not EndOfFile (file) text$ = ReadText (file, true) printr ">" + text$ + "<" wend
' Close file now that we're finished with it CloseFile (file)
Hope this helps. (Otherwise, you could describe the file format, and I'll write up a more customised example.) -Tom
|
|
Voodoo cant remember pass
Guest
|
Post by Voodoo cant remember pass on Mar 9, 2004 11:45:49 GMT -5
ah, that should help a LOT. thanx man. i didn't see the file io help in the documentation, hm, i'll go look again, but this info should really help. thx again.
|
|
Voodoo forgot pass again
Guest
|
Post by Voodoo forgot pass again on Mar 10, 2004 22:47:58 GMT -5
hm, still isn't working. i'm reading from the text file, converting it to an integer, and am trying to make an array based off that data, but i keep getting "array size must be zero or greater". heres my code:
dim numoftris dim thefile
thefile = openfileread("test.txt")
seek(thefile,0)
numoftris = val(readtext(thefile,true))
dim tris#(numoftris-1)(2)(2)
dim t, i
for t=0 to numoftris-1 for i=0 to 2 tris#(t)(i)(0) = val(readtext(thefile, true)) tris#(t)(i)(1) = val(readtext(thefile, true)) tris#(t)(i)(2) = val(readtext(thefile, true)) next next
closefile (thefile)
glcolor3f(1,1,1) for t=0 to numoftris-1 glbegin(gl_triangles) for i=0 to 2 glvertex3f(tris#(t)(i)(0),tris#(t)(i)(1),tris#(t)(i)(2)) next glend() next
swapbuffers()
the .txt file is just: 1 1 2 0 3 6 0 4 6 0
exactly like that. any help is always appreciated, thx all.
|
|
|
Post by Tom Mulgrew on Mar 11, 2004 3:27:59 GMT -5
You're almost there.
You need to put the test.txt file in the "Files" subdirectory from where your program is stored. And then open it with
thefile = openfileread("files\test.txt")
You'll probably also find that you need a
glTranslatef (0, 0, -20)
line to make the triangle visible when it draws.
Btw, I often find it useful to put the following code after a file open:
if FileError () <> "" then print FileError () end endif
Then if it fails for some reason it will print out what's wrong.
-Tom#nosmileys
|
|
|
Post by Voodoo on Mar 11, 2004 10:45:29 GMT -5
beautiful man, just beautiful. thx a bunch man, it's working now. can i ask why the text file has to be in a subdirectory called "files", and can the subdirectory be called anything as long as there is at least one subdirectory? thx again for the help, much appreciated. later.
|
|
|
Post by Tom Mulgrew on Mar 11, 2004 20:46:37 GMT -5
It's basically a (somewhat experimental) security thing. To prevent people writing malicious Basic4GL programs. It means that people can't write programs that overwrite important files on your hard drive, and also later - when network support is implemented - it prevents programs from scanning your hard drive for sensitive information and transmitting it over the net.
This way the only damage a Basic4GL program can do is inside a small subdirectory called files. So if you download a prog from someone you don't know (or don't trust!) you don't have to check over the source code before running it.
As I said, it's still a bit experimental at the moment. I may change the restriction to "everything in the current directory or below".
-Tom
|
|