Fast .csv file reading


Thread Starter


Dear All,
i have a problem. Our Scada system makes the arhive file from the database into a .csv file.
Have somebody a fast algoritm or example (Vb, Deplhi or C++)to read a csv file, Does somebody know how the Excel works with .csv file?
Thank You!

Best regards!
You need a parser. This parser needs to be aware of the different data types delimited by the comas (strings, ints, floats...). As it parses the data in the csv file, you need to process it in some way. Add it to an Access table? Calulate statistical data?

Paul K. P.E.

.CSV is native to Excel. All you have to do is double-click on the file and it should open immediately in Excel. If it doesn't, you might have to associate it in Windows to Excel.

Once in Excel, it is very easy to sort, chart, and analyze your data. Look up "Macros" in the Excel help file if you need to automatically do some data manipulation.

It's real simple. CSV stands for comma separated values.
It's simply a text file, with a format like:

Steve Myres, PE
Automation Solutions
(480) 813-1145

Michael Griffin

"Reading" a CSV file is never a problem. The question is what are you trying to do with it? Are you trying to convert it to a different format, analyse data in it, decimate data, or something else?

Brian Boothe

<p>Provided by
Private Sub makefile()
On Error Resume Next
c = 0
Open app.Path & "\LISTEX.csv" For Append As #1
Write #1, c, Label1, Label2, Label3, Label4, Label5, Label6, Label7, Label8
Close #1

Do While Not Adodc1.Recordset.EOF
c = c + 1

Call textfile

If Adodc1.Recordset.EOF = True Then Adodc1.Refresh
MsgBox "Your csv file is ready. CLICK 'OPTIONS / View csv File to see the
file you have created."
Frame1.Visible = False
End Sub

Private Sub textfile()
'allows you to make headers
Open app.Path & "\LISTEX.csv" For Append As #1

Write #1, c, Adodc1.Recordset.Fields(Label1.Caption),

Close #1

End Sub

<p>This snip is perfect for CSV files that you want to open in Excel:
strTmp As String
Open strPath For Output As #1
RecordsetToCSV = False

Print #1, """"
Do Until rs.EOF
' shoots blocks of 100 rows for speed
tmp = rs.GetString(, 100, """,""", """" & vbCrLf & """", "")
If rs.EOF Then
' drop the extra double quoted character sent
tmp = Left$(tmp, Len(tmp) - 1)
End If
Print #1, tmp;
RecordsetToCSV = True

End FunctionClose #1

Brian Boothe

<p>Here in Delphi provided by
procedure TForm1.FileLoadClick(Sender: TObject);
FileName1, sRecord: string;
Row: integer;
// Let user select a file.
// Skip this until next lesson. For now, simply hard-code it.
FileName1 := 'C:\ParseCSV\Office.csv';

ListBox1.Items.LoadFromFile(FileName1); // 2.
StringGrid1.RowCount := ListBox1.Items.Count; // 3.

// for every record... ( count starts at 0 ! )
for Row := 0 to ListBox1.Items.Count - 1 do begin
sRecord := ListBox1.Items[Row];
ParseRecord(sRecord, Row); // 4.

// 5. Select first "data" cell
StringGrid1.Row := 1;
StringGrid1.Col := 0;
But of you open this file directly in to excel scada will stop updating it I have tried this... you need some sort of quries to retrive that data from file first.

Martin Severgnini

Hi Zsolt.

We develop some programs to handle P-CIM csv files generated from .his files. Please contact me at [email protected] for more information.


Jagus Automation Inc.

Michael Griffin

This is likely because two different programs are each trying to gain exclusive access to the same file. This is likely because both programs are
asking for write privileges. The operating system is only allowing one program to get exclusive access. You need to work on a copy of the file.

Ideally, the program creating the file should close the file and start a new one after a defined interval (e.g. every hour). The program reading the file can then open any that are not currently being written to (not from the current time interval).
As has been said upthread, what are you trying to do with it? As you can see, there's no point asking for help if we don't tell us that - you never said in the original query that you need to read it while the other program is still updating it, so nobody considered that (not very difficult) requirement. Instead, we waste time with suggestions that you have already tried and

One suspects that if you write down what you need, with all the details, that the solution will become obvious. If it doesn't, at least you'll have something to show others when you ask for help.

Jiri Baum <[email protected]>
MAT LinuxPLC project --- --- Machine Automation Tools