I have a small amount of data that I want to read from a CSV file.
The data is stored in an array of size rows*cols, the array is zero-based.
I open the file using an fstream object
Expand|Select|Wrap|Line Numbers
- ifstream data;
- data.open(file,ios_base::in);
Expand|Select|Wrap|Line Numbers
- int buf_size=1024*1024*10;
- char *BUF=new(char[buf_size]);
- char *buf=BUF;
Expand|Select|Wrap|Line Numbers
- data.read(buf,(1024*1024*10));
Expand|Select|Wrap|Line Numbers
- int pos;
- arr=matrix(rows,cols);//This forms a zero-based array
- for(int i=0;i<rows;i++){
- for(int j=0;j<cols;j++){
- arr[i][j]=(atof(buf));
- pos=(int)(strcspn(buf,","));
- buf=&buf[pos+1];
- }
- }
633 , 1.47154 , 0.00021,
832 , 1.46705 , 0.00022,
1306 , 1.460342 , 0.00034,
1544 , 1.457424 , 0.00028 ,
However, my data is stored without the commas at the end of each line.
How can I change the code in the last snippet so that I can read the data from the buffer and store it in the correct order?
As it stands now when I run the code it reads the data as follows
633 , 1.47154 , 0.00021
1.46705 , 0.00022 ,1.460342
0.00034 , 1.457424 , 0.00028
0, 0 , 0
The command
Expand|Select|Wrap|Line Numbers
- pos=(int)(strcspn(buf,","));
Any suggestions on how to recitfy this, apart from going into each individual file and adding commas, would be much appreciated.
Thanks.