Store file
BlitzMax Forums/BlitzMax Programming/Store file
| ||
Hi all, Are blitz max types able to store files and if so are there any examples? |
| ||
Hi I suppose you mean if you can save 'types' (and values) on external files and load them back? You should use REFLECTION to read/write objects data. (It's called Serialization) http://www.blitzbasic.com/codearcs/codearcs.php?code=2262 |
| ||
Hi degac currently my program goes likefile = OpenFile (cmdpra) While Not Eof (file) AddTextAreaText (cmdout, ReadLine (file) + "~n") Wend CloseStream (file) This works but the metadata comes with it. Is there a way for the ReadLine command to go into the memory of a TYPE so that the program will read through the TYPE and deside what to add to the textarea? Basically I just want the file to go into a buffer. |
| ||
Hi, I don't understand clearly your question. What your file you're reading contain exactly? If your file is a .txt file one solution is to use LoadText (or LoadString) Of course you need to check if the file exist (Filetype) the entire textfile is in 'file' |
| ||
A basic example without error checking or parsing the text.Type TMyType Field text:String Method Load(file:String) text = LoadText(file) End Method End Type Local t:TMyType = new TMyType t.Load("text_file.txt") The field "text" will hold the contents of the file after loading, including new lines. |
| ||
Hi all I think Brucey's code example does what I need. Can I then use readline, writeline and a few if statements to get what I need out of the field "text" so that no metadata is displayed on the screen? Right? |
| ||
No, you would have to parse it yourself. And what metadata? What exactly are you trying to load? |
| ||
What kind of metadata are you getting? Is it some type of tags, like "{b}This is Bold{/b}"? If so, then you will need to parse the string and remove the tags. Here is an example. SuperStrict Local s:String = "This is a <b>bold</b> statement. Maybe we can <u>underline</u> the problem." Print StripTags(s) Function StripTags:String(s:String) Local t:String = "" Local i:Int = 0 Local j:Int = 0 Repeat j = s.find("<",i) If j < 0 Then Exit t :+ s[i..j] i = s.find(">",j)+1 Forever If i < s.Length Then t:+ s[i..] Return t End Function |
| ||
@TomToadSuperStrict Local s:String = "This is a <b>bold</b> statement. Maybe we can <u>underline</u> the problem." Print StripTags(s) 'not working Local oops:String = "I forgot the end tag <" Print StripTags(oops) Function StripTags:String(s:String) Local t:String = "" Local i:Int = 0 Local j:Int = 0 Repeat j = s.find("<",i) If j < 0 Then Exit t :+ s[i..j] i = s.find(">",j)+1 Forever If i < s.Length Then t:+ s[i..] Return t End Function I bet it is better to have a boolean "open tag" indicator... and to read tag-parts as text if the tag indicator is on "false". You could fix your code using some checks wether ">" follows on a "<". Also make sure that an "< and now?" does not stumble over negative array indizes. bye Ron |
| ||
Small fix: Will remove everything after the last "<". Nested tags wont crash the program either, but will produce strange results. Could be solved by keeping track of the number of "<" and ">" |
| ||
Better version. Will strip nested tags properly. |
| ||
I had this "fix" too (but wanted to have something for you you to do during the coffee break :-)). Print StripTags(">I am the invisible -m-a-n- text") -> not printed at all bye Ron |
| ||
Can I then use readline, writeline and a few if statements to get what I need out of the field "text" so that no metadata is displayed on the screen? Right? You should probably elaborate more, before these guys spend too much time on some function you don't need :-) |
| ||
You should probably elaborate more, before these guys spend too much time on some function you don't need :-) Too late. Already created an improvement that allows me to escape the < and > characters. :D To keep this thread on track though, I will refrain from posting it. :) |
| ||
Sorry for the delay everyone, I just want to know weather a loadtext object acts in the same way as a file object. Later on I figure out the meta data parsing and mabey play with seeing how many different file types I can parse. But right now the question is does readline work on loadtext like it does on openfile? |
| ||
No, readline only works on streams/files. (read the documentation for ReadLine - it's only parameter is a stream:TStream) LoadText puts the whole file into a string. (again, the documentation discusses what LoadText does). You then need to parse it. You can use something like yourstring.Split("~n") to create an array of "lines", which you can then loop through. If you press F1 on a built-in function, it will show basic help in the status bar at the bottom of the IDE. If you press F1 again, it will display the help page. It's useful to understand how things work. |
| ||
Thank you Brucey. Sometimes the docs are vague and occanaly someone wants to do something that isn't covered by the docs. But anyway I think that I have enought to go on now. |