Read last value from csv file

davidvilla

Member
Hi All,
I have a csv file. When I try reading it through an INPUT STREAM statement and IMPORT it to a temp-table, I am not able to read the last value in the last line of the csv file. When I added a newline character after the last line, I was able to read it. Is there any way around to do this, without touching the csv input file?

Thanks in advance.
 
no, it is not... not if you read it with IMPORT, you can COPY-LOB to a longchar and do your own 'import' but there is a lot of fun in there :)
 
NO-UNDO is not working.

Code:

define stream stMyStream.
define temp-table ttMyTempTable no-undo
field iField1 as integer
field cField2 as character.

input stream stMyStream from "c:/mycsvfile.csv".

repeat:
create ttMyTempTable.
import stream stMyStream delimiter "," ttMyTempTable.
end.

for each ttMyTempTable:
display ttMytempTable.
end.
 
the only difference NO-UNDO can make for import is that if your temp-table is defined with NO-UNDO you'll get a 'ghost' empty record in it because the records gets created before the actual import and fails when there are no more rows.

your issue is that the file is not 'properly' terminated, the last line does not end so it is not seen by progress... you can simply load the content to a longchar, export it with an extra new line at the end to a temporary file and import from that.
 
output to ./t2.txt.
put control "~015".
output close.

unix silent cat ./t2.txt >> ./original.txt.

now your original file has carriage return.
 
Is there any other way to do this without using the temporary variable or temporary file?

sure, if you want/can update the file you can simply add the end-of-file to it before import.

Code:
output to [file] append.
put unformatted '.' skip.
output close.

input from [file].
repeat:
   import.
end.
input close.
 
then you're left with file copy option, try os-copy if you don't like copy-lob... i'll use the session temp folder for internals, make sure you clean up the kitchen table when you're done :)
 
Back
Top