CSV in array

Oct 15, 2008 at 12:41pm
I want to read from a stream and put the data in an (2D)array. The file I'm reading is a CSV-file like the following:
0.1;12345;98
0.2;5006;52
0.3;4540;38
So with every ';' it has to skip column and with every '\n' it has to skip a row. The file is opened through a fileDialogBox.
Can someone give me some advice on how to handle this problem?
See code below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

//Open file dialog
       Stream^ myStream;
       OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;

       openFileDialog1->InitialDirectory = "c:\\";
       openFileDialog1->Filter = "All files (*.*)|*.*";
       openFileDialog1->FilterIndex = 2;
       openFileDialog1->RestoreDirectory = true;
	   
       if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK )
       {
	     if ( (myStream = openFileDialog1->OpenFile()) != nullptr )
	     {
		//Insert code to read the stream here.
		int c;
		FILE * myStream;
								
		c = getc(myStream);
		if(c==";")
		{

		}
		else 
		while(c!=EOF);

Thaanxx

Last edited on Oct 15, 2008 at 12:44pm
Oct 15, 2008 at 1:26pm
c is an int right now and you're calling the get char function to load it? I think you want a char for the semicolon. If you know the format I'd just load in the whole line then do the operations on it.

You can do it simply (although a lil ugly) by pulling in the float, then semicolon, then int, then semicolon, then int again and placing those into the array where you want them. Then just repeat this process until EOF.
Oct 15, 2008 at 2:03pm
Thanks for the reply..
I understand what you mean, but can you give some example code to get me started? Real n00b at work here...
Oct 15, 2008 at 6:41pm
Personally, I'd use the getline() function to read line-by-line from your file into a string. Then you can use string.find() and string.substring() to split the string up based on the location of the ","s
Oct 16, 2008 at 8:28am
Ok, thanks. But could you show me how that is going to look like in code. I'm trying this for days now, but I don't seem to get a good start with it..
Oct 16, 2008 at 10:49pm
Here is some pesudo-code to help ya.

1
2
3
4
5
6
7
8
int lastLocation = 0;
int location = myCSV.find(",");

while (location >= 0) {
 currentPiece = myCSV.substring(lastLocation, location);
 lastLocation = location;
 location = myCSV.Find(",", location); // Might be location+1
}


:)
Oct 23, 2008 at 9:49am
Thanks for the code, trying to work it out..
Topic archived. No new replies allowed.