rookie - need file access help
rookie - need file access help
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?
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
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
data in file is typical csv
"data","data","data"
etc
Re: rookie - need file access help
here's a more 'direct copy' of your way:
(normally you'd either use #PB_Any, or 0 to start wtih)
(Editted to remove quotes to)
(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
Last edited by jassing on Sun Aug 05, 2012 9:18 pm, edited 2 times in total.
Re: rookie - need file access help
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
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
ReplaceString 

Re: rookie - need file access help
Use d$(t) = Trim (d$(t),Chr(34))cbrooks wrote:I tried
Trim (d$(t),Chr(34))
Re: rookie - need file access help
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?
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
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
Re: rookie - need file access help
lol... If you have time, seeing how you would write it may help me learn pb a little.
Many thanks
Many thanks
Re: rookie - need file access help
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$)
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
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:
isn't going to do anything meaningful...
Your code:
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.
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
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$)
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
thanks - very helpful.
Re: rookie - need file access help
@cbrooks:
please use codetags next time to wrap coding lines,
it could be easier to read it.
thk you
please use codetags next time to wrap coding lines,
it could be easier to read it.
thk you
SPAMINATOR NR.1