I'm trying to read/write a CSV file row by row using Lauterbach CMM script, but it's not working.
I thought I can read each row of CSV file as each line if I read csv as normal file, but it's not true, if you use LF characters in the cell data: Reading CSV file line by line will be terminate. I mean the script reads the row as partially.
Can you please let me know to read CSV file row by row?
My code to read CSV row by row :
OPEN #1 &csv_name /Read
WHILE !FILE.EOF(1)
(
READ #1 %LINE &csv_row
PRINT "&csv_row"
)
Can you please provide information on how to read/write CSV file row by row by using a Lauterbach CMM script?
The way you are using OPEN and READ is intended to read one line of a text file. They are not made especially for CSV files. And while CSV files may have a line-feed-character inside a line (when encapsulated in double-quotes), a line of a normal text file simply ends with a line-feed.
So I think you have two options here: Read the file byte by byte as a binary file or load the whole file to the debuggers virtual memory (VM) and read it from there byte by byte.
Option 1 : Read the file byte by byte
OPEN #1 "&csv_name" /Read /Binary
WHILE !FILE.EOF(1)
(
PRIVATE &line
GOSUB getline "1"
RETURNVALUES &line
IF "&line"!=""
ECHO "&line"
)
CLOSE #1
ENDDO
getline:
PARAMETERS &fh
PRIVATE &c &last &line &inquotes
&last=0
&inquotes=FALSE()
READ #&fh &c
WHILE !FILE.EOF(&fh)
(
IF (&last=='"')&&(&c!='"')
&inquotes=!&inquotes
IF (!&inquotes)&&(&c==CONvert.CHAR(0x0A))
(
IF &last==CONvert.CHAR(0x0D)
&line=STRing.CUT("&line",-1)
RETURN "&line"
)
&line="&line"+CONvert.CHAR(&c)
&last=&c
READ #&fh &c
)
RETURN "&line"
Option 2 : Load the whole file to the debuggers virtual memory (VM) and read it from there byte by byte.
PRIVATEn &line &size &pos
&pos=0
&size=OS.FILE.SIZE("&csv_name")
SILENT.Data.LOAD.Binary "&csv_name" VM:&pos
WHILE &pos<&size
(
GOSUB getline "1" "&pos" "&size"
RETURNVALUES &line &pos
IF "&line"!=""
ECHO "&line"
)
CLOSE #1
ENDDO
getline:
PARAMETERS &fh &pos &size
PRIVATE &c &last &line &inquotes
&last=0
&inquotes=FALSE()
WHILE &pos<&size
(
&c=Data.Byte(VM:&pos)
&pos=&pos+1
IF (&last=='"')&&(&c!='"')
&inquotes=!&inquotes
IF (!&inquotes)&&(&c==CONvert.CHAR(0x0A))
(
IF &last==CONvert.CHAR(0x0D)
&line=STRing.CUT("&line",-1)
RETURN "&line" "&pos"
)
&line="&line"+CONvert.CHAR(&c)
&last=&c
)
RETURN "&line" "&pos"