Home » U++ Library support » U++ Library : Other (not classified elsewhere) » How to read from and write to a table in a file (CSV)?
How to read from and write to a table in a file (CSV)? [message #10323] |
Mon, 02 July 2007 09:50  |
$mike{is_here}
Messages: 12 Registered: July 2007
|
Promising Member |
|
|
Hi,
I'm new to Ultimate (which I like very much) and look for a concept idea, i.e. which elements are most suited to use.
Suppose, I have a CSV table like this:
Reading it line-by-line is no problem. E.g. I can store each line as a String or (already decomposed) in a Vector<String> or Vector<Value>. Once I read the table from the file I'm interested in the columns. E.g. I'd like to get column C for further manipulation:
Suppose I changed it into somethig different, like:
I want to assemble a new table for storage, e.g.
I'm not aware that there is something ready-to-use for this purpose, besides ArrayCtrl, may be.
Thank you for your help.
Kind regards, Micha
|
|
|
|
Re: How to read from and write to a table in a file (CSV)? [message #10339 is a reply to message #10325] |
Tue, 03 July 2007 09:23   |
$mike{is_here}
Messages: 12 Registered: July 2007
|
Promising Member |
|
|
Hi Mirek,
Thanks, I used most of your ideas.
Dear all,
Because this post seems to catch some interest please find attached my solution (XP's zip). The functions
String cleanup(String s);
Vector<String> getLines(String s);
String load(char* filename);
bool save(const char *filename, const String& data);
String ToString(const Vector<String>& vs);
Vector<String> transpose(const Vector<String>& vs);
help to convert input.txt
A, B , C ,D
1,10 , 100, 5
2,20 , 200,55
into output.txt
A,1,2
B,10,20
C,100,200
D,5,55
Any suggestion, feedback or comments are welcome. This code so far does just the required minimum. I.e. it will fail when the number of elements vary. I wonder how to take care of such issues, e.g. through throwing exceptions (try, catch) or else?
What would you do?
Kind regards,
Micha
|
|
|
|
Goto Forum:
Current Time: Tue May 13 23:08:11 CEST 2025
Total time taken to generate the page: 0.04346 seconds
|