Greetings all,
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
I create a buffer to store the data
I read the data from the file into the buffer
I now want to read the data from the buffer and store it an an array of size rows*cols
The problem is that with this code there needs to be a comma at the end of each line in order for the buffer to move to the next position and read the data in the correct order. For example
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
means that the first data point on the new line is skipped and the data is not stored correctly.
Any suggestions on how to recitfy this, apart from going into each individual file and adding commas, would be much appreciated.
Thanks.
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
Code:
ifstream data; data.open(file,ios_base::in);
Code:
int buf_size=1024*1024*10; char *BUF=new(char[buf_size]); char *buf=BUF;
Code:
data.read(buf,(1024*1024*10));
Code:
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
Code:
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.
Comment