What code will: Read 110-byte lines (50 bytes,50 bytes,10 bytes) from a text file
into 3 variables: a,b,c (diff. data-types), until a match is found?
File i/o question.
Re: File i/o question.
First how exactly do you define a "match"? E.g. all three elements are of equal content?
In principle, there are no such "specific" language constructs in FB.
(unlike, e.g. VB.NET)
The way to deal with such "specifications", should be split into:
1) load everything (from file) to memory (for speed reasons)
2) split it into lines (searching for LF)
3) maybe its by now possible to compare file-line with comparison-line.
Otherwise, with differing data-types more splitting (see 2) is needed,
before a successful comparison is possible.
There is enough of code like that, already posted in this forum, why
don't you try a search ... (before asking, that is?).
In principle, there are no such "specific" language constructs in FB.
(unlike, e.g. VB.NET)
The way to deal with such "specifications", should be split into:
1) load everything (from file) to memory (for speed reasons)
2) split it into lines (searching for LF)
3) maybe its by now possible to compare file-line with comparison-line.
Otherwise, with differing data-types more splitting (see 2) is needed,
before a successful comparison is possible.
There is enough of code like that, already posted in this forum, why
don't you try a search ... (before asking, that is?).
Re: File i/o question.
read the complete line in a string and then split the string you get like this
then you can convert all these strings to intergers or what else you will. see for that the 'string operations' in the FB-help.
Code: Select all
a=mid(in,1,50)
b=mid(in,51,50)
c=mid(in,101,10)
Re: File i/o question.
Or -- Load a text file into a udt array of ubyte arrays 50, 50, and 10
(Freebasic has a problem loading to a udt array of string fields, whether fixed length strings or fixed length zstrings)
When you have loaded the text file into the udt array of ubyte arrays then transfer the data to a udt of string fields.
The matching, I don't quite understand what you mean.
(Freebasic has a problem loading to a udt array of string fields, whether fixed length strings or fixed length zstrings)
When you have loaded the text file into the udt array of ubyte arrays then transfer the data to a udt of string fields.
The matching, I don't quite understand what you mean.
Code: Select all
#include "file.bi"
type arraytype
as ubyte a(1 to 50)
as ubyte b(1 to 50)
as ubyte c(1 to 10)
end type
type stringtype
as string a,b,c
end type
sub loadfiletoarray(file as string,b() as arraytype)
If FileExists(file)=0 Then Print file;" not found":Sleep:end
var f=freefile
Open file For Binary Access Read As #f
If Lof(f) > 0 Then
Get #f, , b()
End If
Close #f
end sub
Function loadfiletostring(file as string) as String
If FileExists(file)=0 Then Print file;" not found":Sleep:end
var f=freefile
Open file For Binary Access Read As #f
Dim As String text
If Lof(f) > 0 Then
text = String(Lof(f), 0)
Get #f, , text
End If
Close #f
return text
end Function
Sub savefile(filename As String,p As String)
Dim As Integer n
n=Freefile
If Open (filename For Binary Access Write As #n)=0 Then
Put #n,,p
Close
Else
Print "Unable to load " + filename
End If
End Sub
'create a string of digits for example
dim as string * 10*110 g
for n as long=0 to len(g)-1
g[n]=48+rnd*9
next
'save string to text file
savefile("text.txt",g)
'======================== work with text file ===========
var lngth=filelen("text.txt")\sizeof(arraytype) 'get load dimension
dim as arraytype a(1 to lngth)
dim as stringtype s(1 to lngth)
'must load into ubyte arraytype
loadfiletoarray("text.txt",a())
'transfer arraytype to stringtype
for n as long=1 to ubound(a)
for m as long=1 to 50: s(n).a+= chr(a(n).a(m)):next
for m as long=1 to 50: s(n).b+= chr(a(n).b(m)):next
for m as long=1 to 10: s(n).c+= chr(a(n).c(m)):next
next
'now work with stringtype
print "stringtype array elements .a, .b, .c"
for n as long=1 to ubound(s)
print s(n).a
print s(n).b
print s(n).c
print
next
print
'check
print "Check"
dim as string acc
for n as long=1 to ubound(s)
acc+=s(n).a+s(n).b+s(n).c
next
print
print "original text file"
dim as string L= loadfiletostring("text.txt")
print L
print "string from stringtype array"
print acc
print iif(acc=L,"OK","ERROR")
sleep
kill "text.txt"