Author Topic: random write to a .csv file  (Read 2696 times)

0 Members and 1 Guest are viewing this topic.

Offline Spambucket

  • Newbie
  • Posts: 5
    • View Profile
random write to a .csv file
« on: May 08, 2021, 12:56:03 am »
Hello all you QB64 hackers! It's 12:40am and I should have saved this and gone to bed. But as you well know, you have to write code while the fire is hot, so to speak!

I have a single .csv file with 168 rows down and 4 columns across: All cells are variable-length text.
I can DIM Array$(168,4) and then OPEN FILENAME$ FOR INPUT as 1 -  and read the entire .csv file into Array$(168,4) - this works OK.

Here is my problem (and most likely it has a simple solution):

I would like be able to: open the .csv file and WRITE just one record (4 variable-length text columns) back to that same .csv file, and then close the file.
Side note: the .csv file must be able to be open (shared) by other users at the same time.

Any suggestions? Thanks in advance,

- Spambucket (BASIC codeing since '77)

Offline 191Brian

  • Newbie
  • Posts: 91
    • View Profile
    • My Itch page
Re: random write to a .csv file
« Reply #1 on: May 08, 2021, 03:43:52 am »
Hi

Need a bit more info on what the application is. Do you want to add a new record or change one of the existing 4 records? If changing an existing record while sharing the file with other users could be messy, really need a db with record locking. I haven't tested it but I assume while one user has the file open on one else can open it.
Brian ...

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: random write to a .csv file
« Reply #2 on: May 08, 2021, 03:54:00 am »
Well I would read the entire file because it is comma delimited and if there is one comma out of place then there is danger of overwriting a record.  If the data base is under a GIG then read the entire file.

I would rely on software to parse the data and make sure it's correct before changing the record and then write back the entire database. 

Offline euklides

  • Forum Regular
  • Posts: 128
    • View Profile
Re: random write to a .csv file
« Reply #3 on: May 08, 2021, 05:19:20 am »
Could you perhaps show same lines of your csv file ?

For instance: this is a CSV File
a;b;c;d
0.808531006;0.860238524;0.394365025;0.280858343
0.200947441;0.877617412;0.491364623;0.304028333
...
...
...

It could be something like this:

DIM Q$(140,4) ' use alphanumeric variable


reading:
open the file for input
LINY=0:COLUMNY=0
while not eof(*) the file
line input #*,  a$:a$=a$+";"
LINY=LINY+1:COLUMNY=1
Scissors: if len(a$)<=1 then goto Wendix
J=instr(a$,";"):B$=left$(a$,J-1):Q$(liny,Columny)=B$
Columny=Columny+1
a$=right$(a$,len(a$)-j):goto Scissors
wendix: wend:close *

here you change the information
Q$(n,m)= other value  >>>  for instance q$(3,4)=str$(0.888)


here you write back your CSV:

open file.csv for output as **
maxColumn=4
for x=1 to LINY:t$="":for y=1 to maxColumn:t$=t$+q$(x,y)+";":next y
t$=left$(t$,len(y$)-1):print #*, t$
next x
close *
« Last Edit: May 08, 2021, 05:22:51 am by euklides »
Why not yes ?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: random write to a .csv file
« Reply #4 on: May 08, 2021, 09:25:51 am »
Since you use: OPEN FILENAME$ FOR INPUT
the lines are CR+LF separated so use
Open FileName$ For OutPut as #FileNum
to save array back to file.

then For each line of array
For lineNum = lbound(array) to uBound(Array)
   ' cvs = comma separated values so separate array items with commas
   Print #FileNum , Array(lineNum, 1) + "," + Array(lineNum, 2) + "," + ...  + Array(linenum, 4)
Next
Close ect...

BTW Open For Random typically requires Fixed length Records.

Welcome Spambucket
« Last Edit: May 08, 2021, 09:30:43 am by bplus »