read file problem

BlitzPlus Forums/BlitzPlus Programming/read file problem

loonix(Posted 2015) [#1]
how can i store a set of numbers in a txt file so i can read them in to an array?
here is the data

Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 1,1,1,1,1,1,3,3,1,3,3,1,1,1,1
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 0,0,0,0,0,0,0,5,5,0,0,0,0,0,0
Data 1,1,0,2,2,2,2,2,2,2,2,2,2,0,0
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 0,0,0,0,0,0,0,0,0,5,5,3,3,1,1
Data 0,0,1,1,1,1,1,1,1,0,0,0,0,0,0
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1


its for level maps... instead of having loads of data statements in the program
can i read them from a file straight into the array ?

thx in advance


Yasha(Posted 2015) [#2]
Short answer: "yes"

There are honestly any number of ways to do this. You should probably familiarize yourself with the File/Stream command set (ReadInt, ReadLine, etc) so that you know what options are available to you (BlitzPlus provides enough commands for reading any file type, if you do the legwork with them), and then decide on what you want your files to look like. You can store your data in pure binary format, or a human-readable text format; the latter is usually less efficient but gives you many more options for editing because you can just change them by hand in a text editor, rather than having to export from a program.

There are also loaders for several mainstream file types in the code archives. Perhaps one of them would suit your needs?

Finally, for your specific purpose, perhaps the easiest thing to do would be to store your level map as an image (use pixel values for the numbers), and just load it with LoadImage rather than messing about with file formats at all. Transfer it into an array with ReadPixelFast once it's loaded.


loonix(Posted 2015) [#3]
thank you i will go and have a good read up and try out some things.


Midimaster(Posted 2015) [#4]
There is a very simple way to do that with READLINE:

READLINE reads a complete line from a text file. you only have to separate the parameters inside this text line.

TextFile% = ReadFile("data.txt") 
For i=1 to 16
    CurrLine$ = ReadLine( TextFile )
    from%=1
    For j= 1 to 16
         ; find the comma:
         here=Instr("," CurrLine, from)
         if here=0 Then here= 999

         ; extract the text between the commas
         value=Mid(CurrLine, from, here-from)

         ; set a new search point
         from=here+1
Next

CloseFile( TextFile ) 



loonix(Posted 2015) [#5]
well , what can i say,, works wonderfully

I def' need to study more, that's for sure !!

Many thanks once again.


Floyd(Posted 2015) [#6]
A note about making life easy for yourself...
If you know that all the data values are single digits then you can modify the text as follows.

Start with what you have

Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 1,1,1,1,1,1,3,3,1,3,3,1,1,1,1
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 0,0,0,0,0,0,0,5,5,0,0,0,0,0,0
Data 1,1,0,2,2,2,2,2,2,2,2,2,2,0,0
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 0,0,0,0,0,0,0,0,0,5,5,3,3,1,1
Data 0,0,1,1,1,1,1,1,1,0,0,0,0,0,0
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1


Copy/paste into a text editor such as NotePad.

Next, a little editing

1. Replace each "Data " with nothing.
2. Replace each "," with nothing.

The text for your data.txt file is now

000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
111111331331111
000000000000000
100000000000000
000000055000000
110222222222200
000000000000001
000000000000000
000000000553311
001111111000000
000000000000000
111111111111111


In this form the "parsing" for a line of text becomes

    For j= 1 to 16
         value=Mid(CurrLine, j, 1)
         ; save value into array
    Next



loonix(Posted 2015) [#7]
I thought i had it sorted with midimasters code, but it seems not to work no matter what i try,
now ive tried a few more things with your code floyd ... but still not doing it.


Floyd(Posted 2015) [#8]
I started to write about verifying each step. When I got to checking that ReadLine worked I had a look at the code and saw that it uses ReadString, which is the wrong command. You want ReadLine for text.


Midimaster(Posted 2015) [#9]
oh, sorry...
ReadLine is correct!