Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Apr 20, 2017 17:06:29 GMT -6
Hi,
Just a quick one...
I am trying to make a simple text adventure and I am trying to re-create the ability to read data statements...
I see that RCB does not (or doesn't seem) to use the old "read" command. From what I have deduced, rcbasic, requires the text file to be opened (assuming it exists of course) and uses readline$ and writeline$ to either read or write to the opened file. (please correct me if I am wrong?)
The RCB reference explains the commands but does not provide an example.
This is a small program that reads a text file (35 lines long) in an attempt to display it on the screen. The program executes without error but displays nothing. Well, almost nothing... a 'window' appears for a split second and disappears...
FileOpen(1,"command.txt",TEXT_INPUT) while 1 = 1 prints(ReadLine$(1)) if eof(1) then exit while end if wend FileClose(1)
I have tried adding a waitkey at the end of the listing, just to try and catch the display, but it was ignored. In all cases, the program compiled without error, and completed successfully. The 'command.txt" file is just 35 lines of pure text. One word on each line.
Am I doing something wrong or is there a glitch?
J
|
|
|
Post by sdlbasic on Apr 21, 2017 2:58:43 GMT -6
Here you have a working demo.
if FileExists("command.txt") = true then FileOpen(1,"command.txt",TEXT_INPUT) while not EOF(1) print(ReadLine$(1)) wend FileClose(1) end if while not key(27) wend end
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Apr 21, 2017 8:10:55 GMT -6
Your listing does the same as mine, except for the fact, you check to see if the file exists. Unfortunately, it too, produces the same result as mine... I do not know how to attach a text file to this post. Can you create a text file of a single word on each line and run it? I am curious to know if the problem is a Windows, Linux, 32 or 64 bit issue. This is such a simple application but is SO frustrating... Many thanks.
J
|
|
|
File I/O
Apr 21, 2017 8:47:14 GMT -6
via mobile
Post by eyfenna on Apr 21, 2017 8:47:14 GMT -6
Tried to run both versions and set the exit Key to Escape the Programm doesn't react at all. Following does the Job although i'm wondering why while doesn't:
FileOpen (1,"command.txt",text_input) istring $ = "" do istring $ = readline (1) print (istring $) loop until EOF (1) waitkey ()
At least Works in windows
|
|
|
Post by sdlbasic on Apr 21, 2017 9:19:24 GMT -6
In my machine (windows 10 64bits) with the latest rcbasic works the up code by eyfenna and my also works, see the next snippet if it works.
f = freefile() if fileexists("command.txt") = true then fileopen(f,"command.txt",TEXT_INPUT) while not eof(f) print(readline$(f)) wend fileclose(f) end if waitkey()
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Apr 21, 2017 19:25:35 GMT -6
I am beginning to think the problem maybe with the 64 bit version of RCB. Both 'sdlbasic' and 'eyfenna' program ran and compiled without error (minor typo with 'istring $' but easily fixed) but both programs did not produce a display. Maybe Admin can check for a potential bug?
I want to thank both of you for all your assistance with this problem. Your efforts are appreciated. I had hoped to make a simple Text Adventure with all the 'room descriptions' held in an external text file. Not to worry. I'll have to do it 'old school' and assign the data into arrays manually. This is going to be fun...
Many thanks.
J
|
|
|
Post by n00b on Apr 22, 2017 3:05:15 GMT -6
Johnno, what OS are you running. I will try it with your setup to see if I can repeat your results.
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Apr 22, 2017 8:45:40 GMT -6
64 bit Linux Mint 18.0
|
|
|
File I/O
Jul 8, 2017 9:55:32 GMT -6
via mobile
Post by eyfenna on Jul 8, 2017 9:55:32 GMT -6
readline $() crashes in 2.0.8 independent if there is a filestream or there is none. win10 64bit. same with seek() and tell()
any posting before was done after a test with 2.0.7 in which it works.
update: got it working again but heaven knows why. though there seems to be an error if the text file is in a different folder than the *.bas file that accesses it.
|
|
|
Post by n00b on Jul 8, 2017 15:06:30 GMT -6
I have just tested it and it seems to work like it should. I have a few suggestions. First make sure you are opening the file for input if you are using the readline$() function. Second, when you are specifying the location of the file make sure you are separating directories with either "/" or "\\". Using a single "\" is telling the compiler that you are trying to escape something (Refer to the Escape Characters section in the manual). And finally, I need to add this to the manual but FileOpen() will return true if the file opens and false if it could not open the file. You can use this as an error check before reading or writing to the file.
|
|
|
File I/O
Jul 9, 2017 8:44:21 GMT -6
via mobile
Post by eyfenna on Jul 9, 2017 8:44:21 GMT -6
thanks for the tips.
writeline() is not working in usefull ways imho it writes every writeline after each other in the same line. it ignores escape characters and means i have to format my text after writing it to a file by hand. writeline being able to react to \n is a little wish of mine.
|
|
|
Post by n00b on Jul 9, 2017 8:58:35 GMT -6
If you want a line break use the "\n" escape character.
|
|
|
Post by eyfenna on Jul 9, 2017 10:26:04 GMT -6
If you want a line break use the "\n" escape character. You mean like in: fileopen(0,"names.txt", text_output)
dim strings$[5] strings$[0] = "apple\n" strings$[1] = "banana\n" strings$[2] = "cherry\n" strings$[3] = "lemon\n" strings$[4] = "orange\n"
for i = 0 to arraysize(strings,1) -1 seek(0,tell(0)+2) writeline(0, strings$[i]) print(tell(0)) wait(1000) next gets me following text in a *.txt file viewing it with editor: apple banana cherry lemon orange
|
|
|
Post by n00b on Jul 9, 2017 22:43:50 GMT -6
I am going to assume you tried to open the text file up in notepad. If you open it in wordpad or any text editor with margins or formatting on Windows, then you will see the line breaks. This doesn't have anything to do with writeline but instead has to do with how windows does line breaks. On Windows, line breaks are made of 2 characters: a carriage return and a line feed. To insert both of these characters in rcbasic you could just do "\r\n". That is also how you would have to do it in C++, Java, or any other programming language that outputs raw text. The problem with doing it with both characters is that both characters end up doing practically the same thing on every other operating system but Windows. Here is a link to the Wikipedia page on newline so you can read up on it: Newline Wikipedia Page
|
|
|
Post by tbird on Sept 15, 2018 10:05:17 GMT -6
Glad this post was here, I was having the same issue, but I can confirm opening it in another program, or in Linux it shows as it should!
|
|