rookie - need file access help

Just starting out? Need help? Post your questions and find answers here.
cbrooks
User
User
Posts: 57
Joined: Thu Sep 16, 2010 3:26 am

rookie - need file access help

Post 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?
cbrooks
User
User
Posts: 57
Joined: Thu Sep 16, 2010 3:26 am

Re: rookie - need file access help

Post 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
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: rookie - need file access help

Post 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)
Last edited by jassing on Sun Aug 05, 2012 9:18 pm, edited 2 times in total.
cbrooks
User
User
Posts: 57
Joined: Thu Sep 16, 2010 3:26 am

Re: rookie - need file access help

Post 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
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Re: rookie - need file access help

Post by Polo »

ReplaceString :)
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: rookie - need file access help

Post by Demivec »

cbrooks wrote:I tried
Trim (d$(t),Chr(34))
Use d$(t) = Trim (d$(t),Chr(34))
cbrooks
User
User
Posts: 57
Joined: Thu Sep 16, 2010 3:26 am

Re: rookie - need file access help

Post 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?
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: rookie - need file access help

Post 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....
cbrooks
User
User
Posts: 57
Joined: Thu Sep 16, 2010 3:26 am

Re: rookie - need file access help

Post by cbrooks »

lol... If you have time, seeing how you would write it may help me learn pb a little.

Many thanks
cbrooks
User
User
Posts: 57
Joined: Thu Sep 16, 2010 3:26 am

Re: rookie - need file access help

Post 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$)
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: rookie - need file access help

Post 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.
cbrooks
User
User
Posts: 57
Joined: Thu Sep 16, 2010 3:26 am

Re: rookie - need file access help

Post by cbrooks »

thanks - very helpful.
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Re: rookie - need file access help

Post by Rings »

@cbrooks:
please use codetags next time to wrap coding lines,
it could be easier to read it.

thk you
SPAMINATOR NR.1
Post Reply