ReadLine / WriteLine with Prefix Text

Started by Zeotrope, November 08, 2020, 03:55:38

Previous topic - Next topic

Zeotrope

Got a basic question regarding ReadLine / WriteLine.

All i want to do is load 2 variables from a *.txt file like so...

filein_atmosphere = ReadFile("data/atmosphere.txt")

pot4_pos = ReadLine ( filein_atmosphere  ) ; fog minimum
pot5_pos = ReadLine ( filein_atmosphere  ) ; fog maximum

CloseFile( filein_atmosphere )

fileout_atmosphere = WriteFile("data/atmosphere.txt")

WriteLine ( fileout_atmosphere,pot4_pos ) ; fog minimum
WriteLine ( fileout_atmosphere,pot5_pos ) ; fog maximum

CloseFile( fileout_atmosphere )





this reads and writes from a txt file that looks like this....

1
16



What I want is it to read / write from a txt file that looks like this....

min fog; 1
max fog; 16



I have success in creating the WriteFile command such as: WriteLine ( fileout_atmosphere,"fog min; "+pot4_pos ) ; fog minimum
But not sure how to read this into my program. (ie: It just returns a zero value)


Matty

Many ways of doing it however....

What I like doing is this:

writeline outfile,"fogout="+fogoutvariable

and then on reading do this:

val$ = readline(infile)
if instr(val,"fogout=") then
   newval$ = replace(val,"fogout=","")
   if  int(newval) > 0 then
        fogoutvariable = int(newval)
  endif
endif

or similar....

Scaremonger

#2
Using Reflection; you can save/load custom types directly to/from files using something like this (Tested on BlitzmaxNG).

I have only added basic error detection for file access and the functions will only ignore fields prefixed with an underscore "_".


Strict

Type MyFog Extends TConfig
Field minfog:Int = 1
Field maxfog:Int = 16
End Type

Type TPlayer Extends TConfig
Field x:Int
Field y:Int
Field size:Float
Field name:String

Method Load:TPlayer( filename:String )
Return TPlayer( Super.Load( filename ))
End Method

End Type

Print( "FOG" )
Local fog:MyFog = MyFog( New MyFog.Load( "config.txt" ) )
Print( "MIN:  "+fog.minfog )
Print( "MAX:  "+fog.maxfog )

Print( "PLAYER" )
Local player:TPlayer = New TPlayer.Load( "config.txt" )
Print( "NAME: "+player.name )
Print( "X:    "+player.x )
Print( "Y:    "+player.y )
Print( "SIZE: "+player.size)

player.saveas( "player.txt" )

' Config Reflection
' Si Dunford (Scaremonger), Nov 2020
Type TConfig
Field _filename:String

Method Load:TConfig( filename:String )
Local fld:TField
Local tid:TTypeId = TTypeId.ForObject( Self )
Local file:TStream = ReadFile( filename )
If Not file Return Null
_filename = filename

' Parse the config file extracting fields
While Not file.Eof()
Local keyval:String[] = file.ReadLine().split( "=" )
If Len(keyval) <> 2 Continue
fld = tid.findfield( keyval[0] )
If Not fld Continue
fld.set( Self, keyval[1] )
Wend
' Clean up
file.Close()
Return Self
End Method

Method save:Int()
If Not _filename Return False
Local tid:TTypeId = TTypeId.ForObject( Self )
Local file:TStream = WriteFile( _filename )
' Save fields into file
For Local fld:TField = EachIn tid.EnumFields()
If fld.name()[..1] <> "_" file.WriteLine( fld.Name()+"="+String(fld.get(Self)) )
Next
' Clean up
file.Close()
Return True
End Method

Method saveas:Int( filename:String )
_filename = filename
Return save()
End Method

End Type

You need a file called "config.txt" to run the above script:

x=10
y=20
size=3.5
minfog=1
maxfog=16
name=Scaremonger

Derron

Quote from: Scaremonger on November 08, 2020, 09:26:51
Using Reflection; you can save/load custom types directly to/from files using something like this (Tested on BlitzmaxNG).

This is the Blitz2D, BlitzPlus and Blitz3D sub forum.


bye
Ron

GrindalfGames


templine=readline(filein)
minfog=Right$(templine$,Len(templine$)-9);-9 is the amount of characters in "Min fog; " before the numbers begin

I think that would work but I've not tested it. Either way I think you can see what I'm going for there and fix it if I've messed up

Zeotrope

Quote from: Matty on November 08, 2020, 04:56:47
Many ways of doing it however....

What I like doing is this:

writeline outfile,"fogout="+fogoutvariable

and then on reading do this:

val$ = readline(infile)
if instr(val,"fogout=") then
   newval$ = replace(val,"fogout=","")
   if  int(newval) > 0 then
        fogoutvariable = int(newval)
  endif
endif

or similar....

Thanks Matty (and everyone else)
This worked for me...and was the easiest to add to my code

So my Read File looks like this ...

filein_atmosphere = ReadFile("data/atmosphere.txt")

val$ = ReadLine(filein_atmosphere) ; fog minimum
If Instr(val,"fog min =") Then newval$ = Replace(val,"fog min =","")
pot4_pos = Int(newval)

val$ = ReadLine(filein_atmosphere) ; fog maximum
If Instr(val,"fog max =") Then newval$ = Replace(val,"fog max =","")
pot5_pos  = Int(newval)


CloseFile( filein_atmosphere )



And my Write File like so...

fileout_atmosphere = WriteFile("data/atmosphere.txt")
     
      WriteLine ( fileout_atmosphere,"fog min ="+pot4_pos ) ; fog minimum
      WriteLine ( fileout_atmosphere,"fog max ="+pot5_pos ) ; fog maximum

CloseFile( fileout_atmosphere )