Page 1 of 1
rookie - need file access help
Posted: Sun Aug 05, 2012 6:31 pm
by cbrooks
in qb64, to read a sequential file with 6 columns of data, I would use:
i.e.
Row1: a, b, c, d, e, f
Row2: a, b, c, d, e, f
Row3: a, b, c, d, e, f
dim d$(6)
open "c:\dir\file.csv" for input as 1
while not eof(1)
for t=1 to 6
input #1,d$(t)
next t
wend
close #1
translating into PB is a little trickier than expected. how do I re-write the above? Old writing habits are getting in my way i think.
Can anyone do a quick translation for me?
Re: rookie - need file access help
Posted: Sun Aug 05, 2012 6:46 pm
by cbrooks
when I use ReadFile(1, d$(t)) it reads the entire row rather than data1,data2 etc separately one at a time
data in file is typical csv
"data","data","data"
etc
Re: rookie - need file access help
Posted: Sun Aug 05, 2012 6:57 pm
by jassing
here's a more 'direct copy' of your way:
(normally you'd either use #PB_Any, or 0 to start wtih)
Code: Select all
Dim d$(6)
ReadFile(1,"c:\temp\data.txt") ;open "c:\dir\file.csv" For input As 1
While Not Eof(1)
cLine$ = ReadString(1)
For t=1 To 6
d$(t) = Trim(Trim(StringField(cLine$,t,",")),#DQUOTE$) ;input #1,d$(t)
Next t
Wend
CloseFile(1) ;close #1
(Editted to remove quotes to)
Re: rookie - need file access help
Posted: Sun Aug 05, 2012 7:14 pm
by cbrooks
thanks! That worked.
To get rid of the quotes, (in PB the csv is read with quotes around the data)
I tried
Trim (d$(t),Chr(34))
but still I get:
"data"
instead of
data
Re: rookie - need file access help
Posted: Sun Aug 05, 2012 7:32 pm
by Polo
ReplaceString

Re: rookie - need file access help
Posted: Sun Aug 05, 2012 7:38 pm
by Demivec
cbrooks wrote:I tried
Trim (d$(t),Chr(34))
Use d$(t) = Trim (d$(t),Chr(34))
Re: rookie - need file access help
Posted: Sun Aug 05, 2012 10:54 pm
by cbrooks
thanks. Now, can you help me write a .csv file in the same format?
in qb64, to WRITE a sequential file with 3 ROWS and 6 COLUMNS of data, I would use:
i.e.
Row1: a, b, c, d, e, f
Row2: a, b, c, d, e, f
Row3: a, b, c, d, e, f
dim d$(6)
open "c:\dir\file.csv" for output as 1
for t=1 to 3
Write #1,d$(1),d$(2),d$(3),d$(4),d$(5),d$(6)
next t
close #1
Can anyone do a quick translation for me?
Re: rookie - need file access help
Posted: Sun Aug 05, 2012 11:17 pm
by jassing
Code: Select all
Dim d$(6)
CreateFile(1,"c:\dir\file.csv") ;open "c:\dir\file.csv" For output As 1
For t=1 To 3
WriteStringN(1,d$(1)+","+d$(2)+","+d$(3)+","+d$(4)+","+d$(5)+","+d$(6))
Next
CloseFile(1) ;close #1
Note -- that's "your code" translated....
Re: rookie - need file access help
Posted: Sun Aug 05, 2012 11:31 pm
by cbrooks
lol... If you have time, seeing how you would write it may help me learn pb a little.
Many thanks
Re: rookie - need file access help
Posted: Sun Aug 05, 2012 11:37 pm
by cbrooks
since I have 31 variables, this is how i did it:
a$=""
For t=1 To 31
If t=1
a$=d$(t)
EndIf
If t>1
a$=a$+d$(t)
EndIf
If t<30
a$=a$+","
EndIf
Next t
WriteStringN(2,a$)
Re: rookie - need file access help
Posted: Sun Aug 05, 2012 11:43 pm
by jassing
w/o knowing the real-world application, I wouldn't know.. you asked for a specific set of code 'translated' -- in vb, your code does about as much as the pb code I posted...
this VB Code:
Code: Select all
dim d$(6)
open "c:\dir\file.csv" for input as 1
while not eof(1)
for t=1 to 6
input #1,d$(t)
next t
wend
close #1
isn't going to do anything meaningful...
Your code:
Code: Select all
a$=""
For t=1 To 31
If t=1
a$=d$(t)
EndIf
If t>1
a$=a$+d$(t)
EndIf
If t<30
a$=a$+","
EndIf
Next t
WriteStringN(2,a$)
is certainly one way to do it -- but it's not very practical or readable.
Using that method (all data in a one dimensional array) is sort of like a square peg in a round hole.
I would use two dimensional arrays to closer match your data set.
Re: rookie - need file access help
Posted: Sun Aug 05, 2012 11:52 pm
by cbrooks
thanks - very helpful.
Re: rookie - need file access help
Posted: Mon Aug 06, 2012 7:57 am
by Rings
@cbrooks:
please use codetags next time to wrap coding lines,
it could be easier to read it.
thk you