File handling problems

General FreeBASIC programming questions.
ITomi
Posts: 151
Joined: Jul 31, 2015 11:23
Location: Hungary

File handling problems

Postby ITomi » Nov 05, 2020 13:33

Hello!

I develop an RTS game and I make a save/load system for it.
To save, I write the datas of game types into a text file (e.g. X and Y position, etc.), but when I try read these out from the file I get some problems.
First, I make a headline in these files to avoid mix with those files what have similar extension. But I can't read out this string, because the program saves it with apostrophes and I can't read out it with apostrophes:

Code: Select all

dim file as ubyte
dim headline as string
Open "saves\gamesave.txt" For Input As #file
input #file,headline
if headline<>str("*mygamesave*") then 'But *mygamesave* never equal with "*mygamesave*" in the file!!!
        close #file
        print "Bye!"
        sleep()
else
(...)


Second, how can I jump onto the next (or any other) line in the text file? It seems, the Input doesn't jump after executes its own line.
And finally, how can I list these files onto the screen? Only those files, which have similar extensions.

The file handling in FreeBasic seems to be a bit difficult for me, so all useful advices are helpful for me here.
Last edited by ITomi on Nov 05, 2020 14:11, edited 1 time in total.
Imortis
Moderator
Posts: 1739
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: File handling problems

Postby Imortis » Nov 05, 2020 13:53

Instead of Input, use Line Input:
https://www.freebasic.net/wiki/KeyPgLineinputPp

Also, look at the wiki page for EOF:
https://www.freebasic.net/wiki/KeyPgEof

And DIR:
https://www.freebasic.net/wiki/KeyPgDir

Hope this helps.
ITomi
Posts: 151
Joined: Jul 31, 2015 11:23
Location: Hungary

Re: File handling problems

Postby ITomi » Nov 05, 2020 14:21

Hello Imortis, and thank you for your advices!
But what if I wrote many datas into one line and I would like read out only the first one, then jump onto the next line? E.g. I have this data lines:
1,0,0,0,0,20,"unitname",0,0,25,34
280

and I read out the first data of it (in this case this is: 1) and then jump to the next line, which have value: 280?
Is this possible in FB?
Imortis
Moderator
Posts: 1739
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: File handling problems

Postby Imortis » Nov 05, 2020 15:53

Code: Select all

Dim as integer a, b, c, d, e, f, h, i, j, k, l
Dim as string g

Input #File, a, b, c, d, e, f, g, h, i, j, k
Input #File, l


This is not complete code, but it is designed to show how you can use Input. I you include more than one variable in the input line, it will reach each value (using the comma as a separator) into each variable, in the order you give them. Once you hit the end of the line, FB will read the next line when Input is called again. There is no need to manually increment the line you are reading.

It is possible to do that, but not required for your example.
jj2007
Posts: 1827
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: File handling problems

Postby jj2007 » Nov 06, 2020 2:08

ITomi wrote:But what if I wrote many datas into one line and I would like read out only the first one, then jump onto the next line? E.g. I have this data lines:
1,0,0,0,0,20,"unitname",0,0,25,34
280

and I read out the first data of it (in this case this is: 1) and then jump to the next line, which have value: 280?
Is this possible in FB?
One solution would be to read all file content into a string array, e.g. with Recall.bi. Afterwards, you can parse line by line and extract the content you are interested in.

Code: Select all

#include "Recall.bi"
  Dim As string s()
  Dim As integer elements=Recall("saves\gamesave.txt", s())
  For ct As Integer=0 to elements-1
   Print ct,  s(ct) ' print each line
  Next 
ITomi
Posts: 151
Joined: Jul 31, 2015 11:23
Location: Hungary

Re: File handling problems

Postby ITomi » Nov 06, 2020 9:25

Thank you for your answers! I try these.
And anybody have idea how to regain a string with apostrophes? It would be important for me in point of headline of the file. Is there a way in FB to read the apostrophes itself?
grindstone
Posts: 770
Joined: May 05, 2015 5:35
Location: Germany

Re: File handling problems

Postby grindstone » Nov 06, 2020 11:41

It's not quite clear to me: When you say "apostrophes", do you mean apostrophes (') or quotes (") ?

To use quotes as a literal you have to print it as double quotes:

Code: Select all

Dim As String f = "saves\gamesave.txt", unitname = "myUnit", g
Open f For Output As #1

Print #1, "abc"
Print #1, """def"""
Print #1, "1,0,0,0,0,20,"""; unitname; """,0,0,25,34"
Print #1, "280"
Close #1

Open f For Input As #1
Do
   Line Input #1, g
   Print g
Loop Until Eof(1)
Close #1

Sleep
dodicat
Posts: 6765
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: File handling problems

Postby dodicat » Nov 06, 2020 13:03

you could also write #1 to keep the double quotes.
or use chr(34) to add quotes.

Code: Select all



dim as string h="Hello"
write h
var z= chr(34)+h+chr(34)
print z
 z="""hello"""
print z
sleep
 
ITomi
Posts: 151
Joined: Jul 31, 2015 11:23
Location: Hungary

Re: File handling problems

Postby ITomi » Nov 06, 2020 13:11

Oh, sorry, I thought quotes, of course.
And this is it; thank you for your helpful answers, Grindstone and Dodicat!
ITomi
Posts: 151
Joined: Jul 31, 2015 11:23
Location: Hungary

Re: File handling problems

Postby ITomi » Nov 06, 2020 16:36

One more question: when I get a different headline as the valid string value, the program halts instead of jump to another game state.

Code: Select all

dim file as ubyte
dim headline as string
file=freefile
Open "saves\"+str(fn) For Input As #file
input #file,headline
if headline<>"*mygamesave*" then
        close #file
        gamestate=0 '<-Jump to the title screen - but it doesn't jump there!
else
(...)


What is the problem? Do I threat the headline badly? E.g. what if headline get a number or a string without quotes?
grindstone
Posts: 770
Joined: May 05, 2015 5:35
Location: Germany

Re: File handling problems

Postby grindstone » Nov 07, 2020 8:30

Your program obviously hangs when gamestate is set to "0", but the snippet you posted provides too little information to say what's going wrong.

And maybe you should rethink the use of quotes as part of the headline name, because it makes the handling unnecessarily complicate.
RockTheSchock
Posts: 233
Joined: Mar 12, 2006 16:25

Re: File handling problems

Postby RockTheSchock » Nov 10, 2020 13:44

Alternatives could be somthing like SQLite or Firebird as embedded Database: Firebird could also later be easily changed to client/server mode
Squirrel or Lua:
Loading lua tables: https://www.freebasic.net/forum/viewtopic.php?f=7&t=22223&hilit=+Lua#p195498

For saving data you could use Serpent.lua:
http://notebook.kulchenko.com/programming/serpent-lua-serializer-pretty-printer

Or JSON

Return to “General”

Who is online

Users browsing this forum: No registered users and 12 guests