is there a way to read .txt files

Started by wadmixfm, December 09, 2023, 17:57:08

Previous topic - Next topic

wadmixfm

Hello everyone again

is there a command in blitzmax NG to load a text file and read its contents and load the details into an array

i have searched the forums but too no avail

i have seen it can be done with python but i have not programmed in that

lee

Yes its really me :)

Rick

#1
Hey,
  Hello, yes there is and it is not complicated at all, unfortunately I have only had time to see how to write to a "txt" file. :o ;) . But while those who really know answer you, I share with you the 5 lines that I wrote. While you're looking at and trying ReadLine(), LoadStream() and ReadStream() they will surely allow you to do what you need.
Big hug R.-

'== Code ==
vdate:String = CurrentDate()
vtime:String = CurrentTime()
vfile:Int = WriteFile("Last entry.txt")
WriteLine(vfile, "Last entry: " + vdate + " " + vtime)
CloseStream(vfile)
'== End Code ==
== The tRick is that there is no tRick ==

Midimaster

#2
there is...

Global Text:String = LoadText("mytext.txt")
... for reading a complete text file with one code line. And we have...

Global Array:String[]
Array= Text.Split(" ")

... to split the text into an array with all the words. You can also use "~n" as separator to receive an array with text lines.

Global Text:String = LoadText("mytext.txt")
Global Array:String[]
Array = Text.Split("~n")
Print "Text contains " + Array.length + " lines"
For Local t:String = EachIn Array
    Print t
Next
...back from North Pole.

wadmixfm

nice one , i will give that a try :)

thanks guys

lee
Yes its really me :)

wadmixfm

Worked a treat just what i needed to import into my program

Brilliant

Thanks guys
Yes its really me :)

wadmixfm

Midimaster

Just a quick question on arrays......

When you set an array like this

Global myarray:String []

Does this mean the array is self growing

In old basic it used to be

Global myarray$[50]

I understand this means array length is 0 to 49

Is this correct

Lee
Yes its really me :)

Henri

Hi,

Global myarray:String[]
This declares a global string array, but doesn't initialize it yet (aka. reserve memory for the elements).

In Blitzmax you can initialize array elements as you would do it in old basic, or afterwards with New like myarray = New String[50]


myarray = Text.Split("~n")
Split method returns a string array already created and initialized inside the method, that is then stored in the declared global string array placeholder.

In real code you should always check that returned array is valid before proceeding.

If Not myarray Or Not myarray.length Then
 Print "Error: Could not load array"
EndIf


-Henri
- Got 01100011 problems, but the bit ain't 00000001

wadmixfm

Yes its really me :)

Baggey


I Wish the Blitzmax Help was as clear and concise as your explanation.

QuoteHi,

Code Select
Global myarray:String[]
This declares a global string array, but doesn't initialize it yet (aka. reserve memory for the elements).

In Blitzmax you can initialize array elements as you would do it in old basic, or afterwards with New like myarray = New String[50]

 I normally start an Array[1] then resize it when i know the size of the file to load? This goes straight to the point and makes the penny drop for the use of New to resize the array. 8)

We can learn new things everyday.

Kind Regards Baggey
Running a PC that just Aint fast enough!? i7 4Ghz Quad core 32GB ram  2x1TB SSD and NVIDIA Quadro K1200 on Acer 24" . DID Technology stop! Or have we been assimulated!

Windows10, Parrot OS, Raspberry Pi Black Edition! , ZX Spectrum 48k, C64, Enterprise 128K, The SID chip. Im Misunderstood!

Midimaster

Of course, you can write both lines into one:

Global Array:String[] = Text.Split(" ")
The reason why I wrote it this way was to show, that already the call of the Split-Method resizes the array.

So this is also possible:

Global Array:String[] = TextA.Split(" ")
' array has now f.e. 22 elements
...
Array = TextB.Split(" ")
' array has now f.e. 33 elements
...back from North Pole.