Dark Bit Factory & Gravity
PROGRAMMING => Freebasic => Topic started by: Shockwave on December 03, 2007
-
Well, I am stumped, I just can't figure out how to do this.
Let's say for arguments sake that I have a text file that contains an unknown amount of lines.
Each line is a maximum of 80 characters wide and each line terminates in a carriage return character.
Loading a text file is very easy in Freebasic using the file handling commands "open" etc.. This is not really what I want to do.
What I want to do is to take the text file and resource this into the executable, normally the command "#include" is sufficient for my needs, and indeed "#include" will include the text file into my executable, however, compilation fails because of illegal characters.
what I wanted to be able to do was;
DIM SHARED AS STRING TEXT_STUFF
TEXT_STUFF="
#INCLUDE "FILE.TXT"
"
Hopefully you can get my meaning?
Any help gratefully accepted!
-
There's not really a simple solution to that, that I know of. Your option is to write a little program which converts it to an array. You could automatically add that to the batch file you use to do a build.
Jim
-
Could you not use bin2bas? and use a decoder to decode it back to your text?
Drew
ps Sorry if this doesn't help! If it was on a ZXSpectrum I would be able to do it straightaway (I think!).
-
Yes, Drew is right, you can use bin2bas for that, it will save your text with all special characters, like carriage return and end of line.
-
Thanks for your replies.
Bin2bas is a great tool that I use all the time but it doesn't do what I need.. I will need to make something to convert text files into my own format for inclusion by the look of it :)
-
Why not make a small converter prog to add data " to the beginning of each line, and " to the end of the lines. For instance, if you have this in the file:
Hello World!
after running the converter prog it would become
data "Hello World!"
Then after including the converted file, you could use FB's read statement to read it all into a string array
-
I am writing a little resourcer in fact Merick, but I have run into all sorts of problems with it.
Namely that tabs are destroyed and also commas wreck my outputted format. As well as this, I can't seem to generate a clean text file with carriage returns at each line of my code.
For example, if I have a line like this;
hello there world.
This is a test.
Two blank lines above.
Now, for, some, commas.
(Written in notepad and run through my converter here is the output;)
SELECTIONS(0,2)="hello there world.":SELECTIONS(1,2)="This is a test.":SELECTIONS(2,2)="Two blank lines above.":SELECTIONS(3,2)="Now":SELECTIONS(4,2)="for":SELECTIONS(5,2)="some":SELECTIONS(6,2)="commas.":SELECTIONS(7,2)="<END>"
When I wanted;
SELECTIONS(0,1)="hello there world.":
SELECTIONS(1,2)="This is a test.":
SELECTIONS(1,3)="":
SELECTIONS(1,4)="":
SELECTIONS(2,5)="Two blank lines above.":
SELECTIONS(3,6)="Now, for, some, commas.":
SELECTIONS(7,7)="<END>":
Some of it is caused by bugs in my code, other things are caused by my lack of understanding of file handling.
I will plug away at it though and probably post again about this if I fail! (likely).
Cheers for the help though Merick :)
-
...
Namely that tabs are destroyed and also commas wreck my outputted format. As well as this, I can't seem to generate a clean text file with carriage returns at each line of my code.
...
I still think that you can do this with bin2bas ::)
-
Sorry Rbraz, I don't understand how I could use Bin2bas to convert a text file like this;
hello world, this is
a text file.
Into;
arrayname(1,1)="hello world, this is"
arrayname(2,1)="a text file."
Any clues greatfully recieved!
The reason I need to do it is so that someone who has no programming knowledge can edit a text file to create articles for a disk mag which can then be converted into code like above and it would all be compiled with the click of a batch file.
Everything works except formatting the text file.
-
I haven't had freebasic installed for a while now, but I have figured that I would give it a try in blitzbasic.
Dim sections$(100)
filename$ = "helloworld2.txt"
FileSize = FileSize(filename$)
filein = ReadFile(filename$)
lineno = 0
tabsize = 5
Repeat
char = ReadByte(filein)
If char > 31 Then
sections$(lineno) = sections$(lineno) + Chr(char)
Else
If char = 9 Then
For i = 0 To tabsize-1
sections$(lineno) = sections$(lineno) + " "
Next
End If
If char = 13 Then
lineno = lineno + 1 ; carriage returns
char = ReadByte(filein) ; skip next byte
End If
End If
Until Eof(filein)
CloseFile(filein)
For i = 0 To lineno-1
Print sections$(i)
Next
WaitKey
End
Here is the content of the textfile used:
hello there world.
This is a test.
Two blank lines above.
Now, for, some, commas.
I have attached all the files that I have made for this example. The second text file shows that tabs can be used as well. I hope its useful somehow even though its in blitzbasic.
-
Thanks Zawran, I feel like I am wasting lots of people's time here, I apologise because loading the file is apparently not a problem.
I need to save the file out again as pure text.
Here's my program so far;
#INCLUDE "WINDOWS.BI"
OPTION STATIC
OPTION EXPLICIT
DIM SHARED AS INTEGER F1
F1=FREEFILE
IF( OPEN( "text.fil" FOR BINARY AS #F1 ) ) THEN
PRINT "ERROR: FILE NOT FOUND:" ; "file.fil"
PRINT "* PRESS ESCAPE TO EXIT *"
WHILE(GETASYNCKEYSTATE(VK_ESCAPE)<> -32767)
WEND
END
END IF
dim shared as string tst
dim shared as integer tick =0
dim shared as integer tick2=2
DIM SHARED AS STRING BOLLOCKS(5000)
DO UNTIL EOF( F1 )
dim as string inpt
INPUT #F1, inpt
BOLLOCKS(tick)="SELECTIONS("+STR(TICK)+","+STR(TICK2)+")"+"="+chr$(34)+inpt+chr$(34)+":"
tick=tick+1
BOLLOCKS(tick)="SELECTIONS("+STR(TICK)+","+STR(TICK2)+")"+"="+chr$(34)+"<END>"+chr$(34)
LOOP
CLOSE #F1
dim as integer ps,l
ps=1
OPEN "include1.txt" for BINARY as #F1
for l=0 to tick
PUT #F1,PS,BOLLOCKS(l)
PS=PS+len(bollocks(L))
next
CLOSE #F1
PRINT "DATA FILES CONVERTED AND SAVED."
PRINT "PRESS ESCAPE TO EXIT."
WHILE(GETASYNCKEYSTATE(VK_ESCAPE)<> -32767)
WEND
END
It produces the result I posted above but doesn't do the job right yet.
Thanks for trying to help me Zawran :) Have some of the good Karma.
-
This example loads the "helloword.txt" file and then saves it back out. It does not support saving tabs because you would have to store that somehow which the loading does not, it just adds spaces. But as you can see, I am writing the two bytes after each string which is the carriage returns. Using this example it reads the file and stores it just as it was and its the same size and everything.
Dim sections$(100)
loadname$ = "helloworld.txt"
savename$ = "helloworldsaved.txt"
filein = ReadFile(loadname$)
lineno = 0
tabsize = 5
Repeat
char = ReadByte(filein)
If char > 31 Then
sections$(lineno) = sections$(lineno) + Chr(char)
Else
If char = 9 Then
For i = 0 To tabsize-1
sections$(lineno) = sections$(lineno) + " "
Next
End If
If char = 13 Then
lineno = lineno + 1 ; carriage returns
char = ReadByte(filein) ; skip next byte
End If
End If
Until Eof(filein)
CloseFile(filein)
For i = 0 To lineno
Print sections$(i)
Next
fileout = WriteFile(savename$)
For i = 0 To lineno
For ii = 1 To Len(sections$(i))
char = Asc(Mid(sections$(i),ii,1))
WriteByte(fileout,char) ; write the character
Next
If i <> lineno Then
WriteByte(fileout,13) ; write the carriage return
WriteByte(fileout,10)
End If
Next
CloseFile(fileout)
WaitKey
End
[edit] I forgot to mention, that if you write a byte value of 9 to the text file, that will then represent a tab when shown in notepad. As you can see in the loading code, thats what I check for when adding the tab spaces.
-
Here's a little example, since fb "print" command automatically recognize carriage return, end of line, tab, this code is minuscule.
For using with your graphic engine, you just need to check each byte for CR,LE,TAB (etc...) and for example if you find a "&H0D,&H0A" sequence it means a carriage return and line end, you just add a line space to your text writer (YPOS+1) and Zero to your text XPOS.
For TAB, if you find &H09 byte, just add 8 to your XPOS text writer.
And so on...
Hope that help :)
-
Going to pick through these tonight.
Thanks guys.
-
For some reason, it's not writing out exactly what you are sending - either because you are writing ascii to a binary file, or binary to an ascii file.
In Windows, line endings are denoted by the two characters 13 and 10. So for it to come out right in notepad you need to send both of those. In Linux (where I think FB is ported from) you only have a 10.
Check out this http://www.freebasic.net/wiki/wikka.php?wakka=KeyPgOpen (http://www.freebasic.net/wiki/wikka.php?wakka=KeyPgOpen)
I think instead of opening the file as 'Binary', you need to open it as 'output ascii'.
Jim
-
Shockwave, make it easy on yourself and use escape sequences. Take this line from the code you posted:
BOLLOCKS(tick)="SELECTIONS("+STR(TICK)+","+STR(TICK2)+")"+"="+chr$(34)+inpt+chr$(34)+":"
and add + !"\r\n" to the end of it (the ! is what tells fb to use the escape sequence in the string):
BOLLOCKS(tick)="SELECTIONS("+STR(TICK)+","+STR(TICK2)+")"+"="+chr$(34)+inpt+chr$(34)+":"+!"\r\n"
haven't figured that part about preserving leading space/tabs yet though
*edit*
got it, change
INPUT #F1, inpt
to
LINE INPUT #F1, inpt
Here's a the output generated by your original code with these two changes to it:
SELECTIONS(0,2)="This is a test file.":
SELECTIONS(1,2)=" This could contain your data.":
SELECTIONS(2,2)=" But instead it just has a bunch of jibberish!":
SELECTIONS(3,2)=" ":
SELECTIONS(4,2)=" ghsfdghsd":
SELECTIONS(5,2)=" sfhs":
SELECTIONS(6,2)=" hgdfhgdf":
SELECTIONS(7,2)=" fhdf":
SELECTIONS(8,2)=" gfd":
SELECTIONS(9,2)=" fg":
SELECTIONS(10,2)=" hfd":
SELECTIONS(11,2)=" g":
SELECTIONS(12,2)=" g":
SELECTIONS(13,2)="<END>"
-
Cheers Jim :)
-
And cheers Merick :)
Karma all round!
-
Just realised that I never posted the solution to this, sorry!
Here's what I ended up with.
It worked well enough.
'By shockwave.
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#INCLUDE "WINDOWS.BI"
OPTION STATIC
OPTION EXPLICIT
DIM SHARED AS INTEGER F1
F1=FREEFILE
IF( OPEN( "p1.txt" FOR BINARY AS #F1 ) ) THEN
PRINT "ERROR: FILE NOT FOUND:" ; "p1.txt"
PRINT "* PRESS ESCAPE TO EXIT *"
WHILE(GETASYNCKEYSTATE(VK_ESCAPE)<> -32767)
WEND
END
END IF
dim shared as string tst
dim shared as integer tick =0
dim shared as integer tick2=1
DIM SHARED AS STRING BOLLOCKS(5000)
DO UNTIL EOF( F1 )
dim as string inpt
LINE INPUT #F1, inpt
BOLLOCKS(tick)="SELECTIONS("+STR(TICK)+","+STR(TICK2)+")"+"="+chr$(34)+inpt+chr$(34) +chr$(13)+chr$(10)
tick=tick+1
BOLLOCKS(tick)="SELECTIONS("+STR(TICK)+","+STR(TICK2)+")"+"="+chr$(34)+"<END>"+chr$(34) +chr$(13)+chr$(10)
LOOP
CLOSE #F1
dim as integer ps,l
ps=1
OPEN "include1.txt" for BINARY as #F1
for l=0 to tick
PUT #F1,PS,BOLLOCKS(l)
PS=PS+len(bollocks(L))
next
CLOSE #F1
END