GLBasic forum

Main forum => GLBasic - en => Topic started by: kokomodrums on 2008-Jun-14

Title: Simple Question
Post by: kokomodrums on 2008-Jun-14
I'm going to set up a hypothetical in order to figure out how to do something.

Hypothetically speaking:

----------
So I have a .txt file that says 'Hello World'. I want to write a simple program that can read that file, extract 'Hello World' and display it in the program.
----------

Basically, I want to make a program that can read outside data and then interpret it into actual GLBasic code. Any ideas?
Title: Re: Simple Question
Post by: Neurox on 2008-Jun-15
Hi,
This is very simple question  =D
See the follow code:
Code (glbasic) Select

LOCAL Wstr$,NomeFile$
NomeFile$ = "hello.txt"       //Filename
OPENFILE(1, NomeFile$, TRUE)  //Open file
READLINE 1, Wstr$             //read first line into text variable
CLOSEFILE 1                   //Close file
PRINT Wstr$,0,0               //print variable
SHOWSCREEN
KEYWAIT


Bye bye
Neurox
Title: Re: Simple Question
Post by: kokomodrums on 2008-Jun-15
Alright, well that works great as far as the basics. So I took that and expanded it a bit and now i'm coming up with a problem. I made a little script that reads the text file line by line then uses each line in the program to load a picture and its details. However, when I run the program, it just closes without doing anything. Help?

------------------
LOCAL mapid,mapname$,mapimage$
OPENFILE(1, "maps.txt", TRUE)
READLINE 1, mapid
READLINE 2, mapname$
READLINE 3, mapimage$
CLOSEFILE 1
PRINT mapid+" "+mapname$,100,100
MOUSEWAIT
LOADBMP mapimage$
SHOWSCREEN
PRINT mapid+" "+mapname$,100,100
MOUSEWAIT
END
------------------

Here is the txt file "maps"

------------------
1
Adair
adair.bmp
------------------
Title: Re: Simple Question
Post by: Moru on 2008-Jun-15
First of all you need to activate debugging, that is the button two buttons to the left of the startbutton. That way you will get an error message when something fails. As it is now it just closes the window as soon as something goes wrong.

Second: the first parameter after READLINE is the channel number, not line-number. If you open the file as channel 1, you need to READLINE 1, str$. That will automagicaly read the next line each time you use that command. (See the code blocks below)

Third: When commenting pieces of code, use the code-button in the forum, this creates the cute blue boxes with code as below :-)

Code (glbasic) Select

OPENFILE(1, "maps.txt", TRUE)
READLINE 1, mapid
READLINE 1, mapname$
READLINE 1, mapimage$
CLOSEFILE 1


Or mabe even make a variable to keep track of what file-nr you are using:

Code (glbasic) Select

LOCAL filenr = 5
OPENFILE(filenr, "maps.txt", TRUE)
READLINE filenr, mapid
READLINE filenr, mapname$
READLINE filenr, mapimage$
CLOSEFILE 1