Read and write CSV files.
Here you will find examples to use csv.
Read and write CSV files. The following rules apply to the format of the file:
A very simple example with ',' as column separator.
aaa,bbb\,BBB
123,0xDEAEDBEEF
123.456,1.23456E+02
static CSVFile open(
char* path,
char* separator,
int autoSave = 0,
int useGlobal = 1)
Open a CSV file.
Name | Type | Default | Description |
Return | CSVFile | 0 : Fehler otherwise : Opened CSV file for further use in the csv functions, see the descriptions of the parameter useGlobal. |
|
path | char* oder String | - | Full path of the CSV file. The path may begin with a $ALIAS name. |
separator | char* oder String | - | Separator used in the CSV file. Separators may be longer than one letter. The only condition is that the separator does not contain line separators (\n or \r). If the separator is to be used in the content, it must be escaped by a single backslash (\) before the separator string. |
autoSave | int | 0 | Should the file be saved automatically before closing? 0 : No 1 : Yes |
useGlobal | int | 1 | Open file globally or only for the current script? 0 : Open file only for the current script. In this case the received result of the function must be closed again with close! 1 : Open file globally. The CSV file remains open until the current data connection is closed or a until call to close. If the file has already been opened in a previous script, this already opened file is returned. Since you probably want to use the file in multiple scripts, this is the recommended procedure. |
static int lines(CSVFile csvf)
Number of lines in file.
Name | Type | Default | Description |
Return | int | > 0 : Number of lines 0 : Empty -1 : Error |
|
csvf | CSVFile | - | With open opened CSV file. |
static int cols(CSVFile csvf, int line)
Number of columns in a given line of the file.
Please note that lines can have different numbers of columns.
Name | Type | Default | Description |
Return | int | > 0 : Number of columns in given line 0 : Empty -1 : Error |
|
csvf | CSVFile | - | With open opened CSV file. |
line | int | - | 0-based line number |
static int read(
CSVFile csvf,
int line,
int col,
int type,
char* value,
int maxlen = 0)
Read a value from a CSV file.
Name | Type | Default | Description |
Return | int | 0 or ErrorCode | |
csvf | CSVFile | - | With open opened CSV file. |
line | int | - | 0-based line in file |
col | int | - | 0-based column in line |
type | int | - | Data type of content in CSV file? 1 - kInt : Integer 2 - kFloat : Float 3 - kString : Text |
value | int*, float* char*, String | - | The data type of the variable depends on the value of the parameter type: 1 - kInt : &int_var 2 - kFloat : &float_var 3 - kString : char* or String |
maxlen | int | 0 | Only used for char* : Maximum length of the result. 0 : Any |
static int write(
CSVFile csvf,
int line,
int col,
int type,
char* value,
int maxlen = 0)
Write a value into a CSV file.
Name | Type | Default | Description |
Return | int | 0 or ErrorCode | |
csvf | CSVFile | - | With open opened CSV file |
line | int | - | 0-based line in file |
col | int | - | 0-based column in line |
type | int | - | Data type of the parameter value 1 - kInt : Integer 2 - kFloat : Floating 3 - kString : Text |
value | int*, float* char*, String | - | The data type of the variable depends on the value of the parameter type: 1 - kInt : int 2 - kFloat : float |
static int save(CSVFile csvf)
Save a CSV file. The file remains open.
Name | Type | Default | Description |
Return | int | 0 or ErrorCode | |
csvf | CSVFile | - | With open opened CSV file |
static int close(CSVFile csvf)
Close a CSV file and remove the object from memory.
The function also closes globally opened files and should therefore only be used for globally opened files in exceptional cases!
Reading from a CSV file
#include "internal/types.h" #include "internal/text.h"
int main () { String str = string::alloc (); CSVFile csv; int vi; float vf; String vs = string::alloc (); int r, c; int convqu = prefs::get_convert_quotas ();
// Open the CSV file // csv = csv::open ("$DESKTOP/aaa.csv", ","); if (!csv) { showmessage ("Cannot open CSV file"); string::release (str);
return 0; }
prefs::set_convert_quotas (kOff);
// Load some data here // frame::replace (gFrame, ""); for (r = 0; r < csv::lines (csv); r++) { for (c = 0; c < csv::cols (csv, r); c++) { string::set (str, "%%!TT_html_[%d, %d] : <b>", r, c); if (csv::read (csv, r, c, kInt, &vi) == 0) { string::append (str, "%d", vi); } else if (csv::read (csv, r, c, kFloat, &vf) == 0) { string::append (str, "%.6f", vf); } else if (csv::read (csv, r, c, kString, vs) == 0) { string::append (str, "'%s'", vs); }
string::append (str, "</b>"); string::append (str, " "); frame::append (gFrame, str); frame::append (gFrame, "\n"); } frame::append (gFrame, "\n"); }
// Clean up // string::release (str); csv::close (csv);
prefs::set_convert_quotas (convqu);
return 0; }
Writing to a CSV file
#include "internal/types.h" #include "internal/text.h"
int main () { CSVFile csv; int r; String str = string::alloc ();
// Open the CSV file // csv = open ("$DESKTOP/bbb.csv", ","); if (!csv) { showmessage ("Cannot open CXSV file"); string::release (str);
return 0; }
// Write some data here // string::set (str, "Your change"); for (r = 1; r < 11; r++) { csv::write (csv, r, 0, kString, "28.10.2018, 12:34"); csv::write (csv, r, 1, kString, str); csv::write (csv, r, 2, kInt, r); string::append (str, " ä"); }
// Clean up // csv::save (csv); csv::close (csv);
return 0; }
Alphabetic index HTML hierarchy of classes or Java