Ooops
January 21, 2021, 06:08:55 AM

Author Topic: ReadLine / WriteLine with Prefix Text  (Read 248 times)

Offline Zeotrope

  • Jr. Member
  • **
  • Posts: 17
ReadLine / WriteLine with Prefix Text
« on: November 08, 2020, 03:55:38 AM »
Got a basic question regarding ReadLine / WriteLine.

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

Code: [Select]
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....

Code: [Select]
1
16


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

Code: [Select]
min fog; 1
max fog; 16


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


Offline Matty

  • Hero Member
  • *****
  • Posts: 1339
    • MattiesGames
Re: ReadLine / WriteLine with Prefix Text
« Reply #1 on: November 08, 2020, 04:56:47 AM »
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....
I eat cheese in the trees when its eight degrees

Offline Scaremonger

  • Full Member
  • ***
  • Posts: 111
    • ITSpeedway - Ramblings of a geek!
Re: ReadLine / WriteLine with Prefix Text
« Reply #2 on: November 08, 2020, 09:26:51 AM »
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 "_".

Code: [Select]
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:
Code: [Select]
x=10
y=20
size=3.5
minfog=1
maxfog=16
name=Scaremonger
Follow me at ITSpeedway.net.

Offline Derron

  • Hero Member
  • *****
  • Posts: 3332
Re: ReadLine / WriteLine with Prefix Text
« Reply #3 on: November 08, 2020, 11:42:18 AM »
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

Offline grindalf

  • Full Member
  • ***
  • Posts: 159
Re: ReadLine / WriteLine with Prefix Text
« Reply #4 on: November 08, 2020, 01:32:20 PM »
Code: [Select]
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

Offline Zeotrope

  • Jr. Member
  • **
  • Posts: 17
Re: ReadLine / WriteLine with Prefix Text
« Reply #5 on: November 09, 2020, 10:58:50 AM »
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 ...

Code: [Select]
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...
Code: [Select]
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 )

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal