You are not logged in.
Pages: 1
I have a "test.csv" file that only contains:
qty,price
4,50
12,100
I'm a beginner in mORMot2 and I'm trying to use the "DynArrayLoadCsv" function, but it always returns false:
type
TCsvItem = packed record
qty: integer;
price: integer;
end;
TCsvItems = array of TCsvItem;
var
csvItems: TCsvItems;
csvContent: RawUtf8;
csvContent := RawUtf8FromFile('test.csv');
if DynArrayLoadCsv(csvItems, csvContent, TypeInfo(TCsvItems)) then
writeln('DynArrayLoadCsv OK')
else
writeln('DynArrayLoadCsv Failed'); I tried changing the delimiter in the csv file from "," to ";" but it's still the same.
This is the complete code for the simple test program, including the CSV file.
I would appreciate any guidance you can provide.
Offline
initialization
// Register RTTI before any CSV parsing
Rtti.RegisterFromText([TypeInfo(TCsvItem), _TCsvItem]);
https://github.com/zen010101/Claude_Mor … array-demo
Last edited by zen010101 (2025-12-19 05:03:29)
Offline
initialization
// Register RTTI before any CSV parsing
Rtti.RegisterFromText([TypeInfo(TCsvItem), _TCsvItem]);
Thank you so much for your help and explanation, and the example you did with Claude is great.
One small detail I encountered with your example when opening it with FPC is that the executable path is in the "bin" folder, while the "test.csv" file is in the root directory.
And obviously, a message appears indicating that it can't be found.
The generated documentation is excellent: very concise and detailed in your example.
Thank you for taking the time to provide the solution and create an excellent example.
Offline
on the program dir, run "./bin/CsvDynArrayDemo.exe" command in the terminal window. ![]()
Offline
Currently, the "DynArrayLoadCsv" function only allows csv file content to have a delimiter/separator ','.
IMHO, the function should have the option (not mandatory) to specify the delimiter/separator.
Respectfully, I propose changing the definition to: (It does not affect programs that have already been created.)
function TDynArrayLoadCsv(var Value: TDynArray; Csv: PUtf8Char;
Intern: TRawUtf8Interning = nil; CommaSep: AnsiChar=','): boolean;
function DynArrayLoadCsv(var Value; const Csv: RawUtf8; TypeInfo: PRttiInfo;
Intern: TRawUtf8Interning = nil; CommaSep: AnsiChar=','): boolean;And its respective implementation code: (I've only changed the constant ',' to a variable "CommaSep: AnsiChar")
An example of using a CSV file containing the semicolon ';' delimiter/separator:
if DynArrayLoadCsv(csvItems, csvContent, TypeInfo(TCsvItems), nil, ';') then
begin
end;Offline
Please try with
https://github.com/synopse/mORMot2/commit/5e6cc78f3
Offline
Please try with
https://github.com/synopse/mORMot2/commit/5e6cc78f3
Thank you for including it.
Offline
Pages: 1