Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
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 Go to next message
$mike{is_here} is currently offline  $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:

A,B,C
1,2,3
4,5,6


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:

C, 3, 6


Suppose I changed it into somethig different, like:

D,100,200


I want to assemble a new table for storage, e.g.

D,B,C
100,2,3
200,5,6


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 #10325 is a reply to message #10323] Mon, 02 July 2007 09:58 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
You need to handcode.

Some quick tips for parsing / storing:

Stream::GetLine

CParser

Split

Vector< Vector<String> >
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 Go to previous messageGo to next message
$mike{is_here} is currently offline  $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
Re: How to read from and write to a table in a file (CSV)? [message #10470 is a reply to message #10339] Tue, 10 July 2007 23:29 Go to previous message
$mike{is_here} is currently offline  $mike{is_here}
Messages: 12
Registered: July 2007
Promising Member
Hi all,

FYI, as expected as my first trial my example has some shortcomings ...

Confused

Nevermind, every fail can turn out to be a great victory Smile

Micha
Previous Topic: Opening window during Drag-Drop crashes app.
Next Topic: Geom/PlotterCtrl fixed plot
Goto Forum:
  


Current Time: Thu Mar 28 23:39:15 CET 2024

Total time taken to generate the page: 0.01093 seconds