Text file help?

BlitzMax Forums/BlitzMax Programming/Text file help?

Pete Carter(Posted 2012) [#1]
I have been trying this for a while now and have picked up bits of info. Reading and writing text files, Is there a tutorial or anything that can teach me how to use the inbuilt functions in blitzmax. I get the read/write line stuff eof etc, but how do you read/write a line that's data separated by commas or extract just the data you need from a file. I would be happy to write a guide for the tutorials section when I've learnt enough. As I don't think this is covered in detail anywhere.

ascii codes for keys and what gets skipped automatically. how to trim unwanted bit of strings and manipulate text. the best bit of info ive found about this was in the old blitz3d book but not all of the text functions are the same as blitzmax. plus ive no idea about streams?

Last edited 2012


Midimaster(Posted 2012) [#2]
two samples:

Stream=WriteFile("test.txt")
	For i%= 65 To 70
		TextLine$ = TextLine + i +";"
	Next
	WriteLine Stream,TextLine
	WriteLine Stream,"Hello World"
	WriteLine Stream,TextLine
CloseFile Stream


Graphics 800,600

Stream=ReadFile("test.txt")
	TextLine=ReadLine(Stream)
	Print "textline=" + Textline
	From%=1
	For i%=1 To 12
		Upto%=Instr(TextLine,";",From)
		If Upto=0 Then Exit
		Part%=Mid(TextLine,From,Upto-From)
		Print Part
		From=Upto+1
	Next
CloseFile Stream



Pete Carter(Posted 2012) [#3]
Thank you for the examples. I understand how the above code works but can you use anything as a symbol that you have got to the end of a piece of data. Are there ones that are automatic. When the manual says about ascii codes how do you enter them. Do you hold alt on the keyboard and type the code number in? Or do i have wrong idea?

Where did you learn this or did you just play with the commands using the manual? Sorry if i sound stupid but i only learn well from examples of how to do one thing at a time. The examples above are great but most of the stuff in the code archive etc are trying to do much more complicated stuff and are not commented.

Last edited 2012


col(Posted 2012) [#4]

but how do you read/write a line that's data separated by commas or extract just the data you need from a file.



Hiya,

You may find the 'String'.Split method handy here :-

Global Line$ = "A,comma,separated,line" ' <- Simulated ReadLine
Global Parts$[] = Line.Split(",")


Parts will end up as an array of strings that are separated by the comma, and the comma will be removed.


Parts[0] = "A"
Parts[1] = "comma"
Parts[2] = "separated"
Parts[3] = "line"


EDIT:- If youre using the standard MaxIDE, browse to

Help->Language->Strings
Help->Language->Slices

Last edited 2012


Midimaster(Posted 2012) [#5]
What do you mean with 'piece of data'? The ReadLine() will always find a complete line with all datas in it . My Upto% becomes zero, if the last value was found.

The EOF(Stream) function "End of File" knows, when the end of the file is reached. It becomes TRUE.


ASCII only means that the data can be opened by a text editor like a normal *.TXT-File. There is no kind of 'encryption' or 'packing' in the data. Easy to open an manipulate. The format is ideal for beginners, the code archive often shows more robust data bases. Don't worry about this, use an easy one!

remark to my code:

Do you need the algo to read a third party file? Or will you use it to create your own files? The difference between my algo and EXCEL is that my algo expects a last ";" at the end of the line.

My:
23;John;1990;-4.345;male;

Excel:
23;John;1990;-4.345;male


Jesse(Posted 2012) [#6]
there are several ways to store data to a file. The simplest one is the one shown to you by Midimaster.

the first thing you need to understand is that a file is just a stream of bytes and there are command that simplify the process of accessing and writing the stream of data.

for example you can write your data in a record style chunks. a record will usually have a name with a limited number of bytes, a score with a length of an integer etc..
to write a name to a file record with a field size of 20 bytes you can do it like this:
lets say your name is John Doe

name = "John Doe"

but the field needs to be 20 bytes to be written to the file: so you add the rest in spaces using Lset:


name = Lset(name,20)

and just use:
Writestring(file,name) 

to write the name to the file.

To write the score just use:
WriteInt(file,score)


if you keep that format for the whole file, you can access all of the records in a similar way
  name = ReadString(file,20) ' reads 20 bytes of the file
  name = trim(name) ' trims of leading and trailing spaces
  score = ReadInt(file)


another way is to use counters: if you know the first field to write is going to be a string and the second an int and it repeats for every record you can do it like this:
name = "John Doe"
WriteInt(file,name.Length)
WriteString(file,name)
writeInt(file,score)

and to access:
  length:int = ReadInt(file)
  name = ReadString(file,length)
  score = readInt(file)


another way and a bit more complicated is to read and write in bytes. just looking for certain characters for the end of a specific field and combining the bytes to make strings, ints etc...


when you write the data just make sure that you keep a consistency in the way you read and write to a file. and it should be no problem.


to write an ascii code and if you know what ascii code you want to write do it like this:
lets say you want to write a return to the file. the code for return is character 13 in Ascii

WriteByte(file,13)

its really that simple.

Here is a table for all of the Ascii character:
http://www.asciitable.com/
and not all of them are printable

Last edited 2012

Last edited 2012


ziggy(Posted 2012) [#7]
I would recommend you to stay away of ASCII codes to store strings and use Unicode. Set the unicode encoding when setting up the stream (it is just one of the parameters of the function) and write characters (not bytes) if you want to grant compatibility between different localizations.


Pete Carter(Posted 2012) [#8]
thanks guys this is the info i was looking for, i will have a play reading and writing files and see what i can do. I really think this is an area of blitzmax that doesn't have enough info or tutorials. once I've got it all spot on in my mind I will put together a tutorial so that anyone else in my position doesn't have to repeat my questions. thanks again. If anybody here has some tips or ideas please feel free to email me. "petecarteraudio at gmail.com" thanks again

Last edited 2012