I would like to push the below data into a vector using the below code.But my code works only for integers.How can I do this for the below data? Thank you.
My data:
M,0.455,0.365,0.095,0.514,0.2245,0.101,0.15,15
M,0.35,0.265,0.09,0.2255,0.0995,0.0485,0.07,7
F,0.53,0.42,0.135,0.677,0.2565,0.1415,0.21,9
M,0.44,0.365,0.125,0.516,0.2155,0.114,0.155,10
I,0.33,0.255,0.08,0.205,0.0895,0.0395,0.055,7
I,0.425,0.3,0.095,0.3515,0.141,0.0775,0.12,8
F,0.53,0.415,0.15,0.7775,0.237,0.1415,0.33,20
F,0.545,0.425,0.125,0.768,0.294,0.1495,0.26,16
M,0.475,0.37,0.125,0.5095,0.2165,0.1125,0.165,9
F,0.55,0.44,0.15,0.8945,0.3145,0.151,0.32,19
My code:
fp = fopen(argv[1], "r"); //Opening the input file in read mode.
if(!fp)
{
printf("open data source file failed!\n");
goto MAINEXIT;
}
int ivalue;
//extract data from files
while(fscanf(fp,"%d,",&ivalue)!=EOF)
{
printf("Counter-%d\n",counter++);
srcdata.push_back(ivalue); //Pushing value by value into the vector with "," delimiter.
}
if(fp)
fclose(fp);
%dis for integers. Change it to%lffordouble, and change other parts accordingly, for example, changeivaluetodouble.According to your comment in the question, I think you might need something like
while(fscanf(fp,"%s", str)!=EOF) { char* pch = strtok(str, ","); pch = strtok(NULL, ","); // skip first while (pch != NULL) { double d = atof(pch); printf("Counter-%d\n",counter++); srcdata.push_back(d); pch = strtok (NULL, ","); } }