Hi people.
I read the Help but no way, I could not understad how to do it.
I have a .txt file saying:
Alexander William
Paul Tom Clarence Peter
...
"many names"
...
Charles John
and I want to read it into a single string in a way the string result will be:
Alexander Williams Paul Tom Clarence Peter ..... ...... Charles John
I tried with Input# and no way, it just give me:
Paul Tom Clarence Peter
I have the problem too that I do not know how many names my .txt file have.
Any hint?
Thanks!
Read a .txt tabulated
-
- Posts: 375
- Joined: Mar 15, 2015 12:41
-
- Posts: 556
- Joined: Mar 10, 2007 15:44
- Location: Ohio, USA
- Contact:
Re: Read a .txt tabulated
Not tested. But will give you idea...
Code: Select all
Dim as string ReadMe, LongString
'Open your file
do
Line Input #1, ReadMe
if Len(LongString) > 0 then LongString += " "
LongString += ReadMe
Loop until Eof(1)
-
- Posts: 375
- Joined: Mar 15, 2015 12:41
Re: Read a .txt tabulated
Nice!!!
Thanks bcohio!!
Thanks bcohio!!
Re: Read a .txt tabulated
Another general way, if speed should be a concern:
Code: Select all
dim as integer fileHandle = freeFile()
dim as string s
'' open file
open "whatever.txt" for binary access read as fileHandle
'' resize the string to hold data
s = string( lof( fileHandle ), 0 )
'' and get the data from file
get #fileHandle, , s
close( fileHandle )
? s
sleep()
Re: Read a .txt tabulated
I posted this solution, but I quickly removed it because I think that characters like 'LF' and 'CR' are not wished inside the expected string.
If you have to remove them manually afterwards, you can do it automatically thanks to 'Line Input #' when reading the file.
If you have to remove them manually afterwards, you can do it automatically thanks to 'Line Input #' when reading the file.
Re: Read a .txt tabulated
You could try this.
Split an incoming file by chr(10) or chr(13) or both, into an array.
Then use the array to suit your requirements.
Split an incoming file by chr(10) or chr(13) or both, into an array.
Then use the array to suit your requirements.
Code: Select all
#include "file.bi"
sub StringSplit(s_in As String,chars As String,result() As String)
Dim As Long ctr,ctr2,k,n,LC=len(chars)
dim As boolean tally(Len(s_in))
#macro check_instring()
n=0
while n<Lc
If chars[n]=s_in[k] Then
tally(k)=true
If (ctr2-1) Then ctr+=1
ctr2=0
exit while
end if
n+=1
wend
#endmacro
#macro split()
If tally(k) Then
If (ctr2-1) Then ctr+=1:result(ctr)=Mid(s_in,k+2-ctr2,ctr2-1)
ctr2=0
End If
#endmacro
'================== LOOP TWICE =======================
For k =0 To Len(s_in)-1
ctr2+=1:check_instring()
Next k
If ctr Then Redim result(1 To ctr): ctr=0:ctr2=0 Else Return
For k =0 To Len(s_in)-1
ctr2+=1:split()
Next k
'===================== Last one ========================
If ctr2>0 Then
Redim Preserve result(1 To ctr+1)
result(ctr+1)=Mid(s_in,k+1-ctr2,ctr2)
End If
End sub
'standard load into a string
Function loadfile(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(1) > 0 Then
text = String(Lof(f), 0)
Get #f, , text
End If
Close #f
return text
end Function
'create a silly file
dim as string s(0 to 9)={"Any ","list ","of ","words ", "or ","characters ","will ","do ","to ","test "}
dim as long f=freefile
dim as string tmp,tally
open "testout.txt" for output as #f
for n as long=1 to 100
tmp="["
for m as long=1 to 1+int(rnd*9)
tmp+=s(int(rnd*10))
next m
tmp=rtrim(tmp)
tmp+=".]"+chr(13,10)
mid(tmp,2,1)=ucase (mid(tmp,2,1))'capital first letter
tally+=tmp
tmp=""
next n
print #f,tally
close #f
'=========== silly file created =======
dim as string L=loadfile("testout.txt")
print
'print silly file
print L
print
print "Press a key"
sleep
redim as string result()
stringsplit(L,chr(13,10),result()) 'take out any of chr(10),chr(13)
print
print
dim as string ans
'rebuild to requirements, here a space between each line
for n as long=lbound(result) to ubound(result)
ans+=result(n)+" "
next
print ans
sleep
kill "testout.txt"
-
- Posts: 375
- Joined: Mar 15, 2015 12:41
Re: Read a .txt tabulated
Thanks guys!
Seems the first solution that gives me bcohio works fine for me.
I missed something?
Seems the first solution that gives me bcohio works fine for me.
I missed something?
Re: Read a .txt tabulated
No, not at all. They are different approaches which serve for different things, just use the one that suits you better. You're welcome.Luis Babboni wrote:Thanks guys!
Seems the first solution that gives me bcohio works fine for me.
I missed something?