File handling problems

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

File handling problems

Post by ITomi »

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: 1924
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: File handling problems

Post by Imortis »

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: 154
Joined: Jul 31, 2015 11:23
Location: Hungary

Re: File handling problems

Post by ITomi »

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: 1924
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: File handling problems

Post by Imortis »

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: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: File handling problems

Post by jj2007 »

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: 154
Joined: Jul 31, 2015 11:23
Location: Hungary

Re: File handling problems

Post by ITomi »

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: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: File handling problems

Post by grindstone »

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: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: File handling problems

Post by dodicat »

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: 154
Joined: Jul 31, 2015 11:23
Location: Hungary

Re: File handling problems

Post by ITomi »

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

Re: File handling problems

Post by ITomi »

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: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: File handling problems

Post by grindstone »

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: 252
Joined: Mar 12, 2006 16:25

Re: File handling problems

Post by RockTheSchock »

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: viewtopic.php?f=7&t=22223&hilit=+Lua#p195498

For saving data you could use Serpent.lua:
http://notebook.kulchenko.com/programmi ... ty-printer

Or JSON
Post Reply