Read text file contents with spaces in between values

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,<br/>
<br/>
I am trying to read a file and the problem is that when I try to go value by value, it is not working. Here is what the value in the text file is...
" 1 1 0.0034600 0.0038300 0.0044600 0.0055400 0.0070900 0.0120200 0.0163200 0.0208000 0.0275700 0.0297700 0.0000000 0.0450159 0.0000000 0.0163744 0.0000000 -0.0028790 0.0000000 0.0323308 0.0000000 -0.0029788 0.0000000 -0.0026992 0.0000000
0.0000200 0.0000000 0.0376980 0.0000000 0.0292196"
Here is my code, I can only read the "1" and then it doesnt read the next value "1" and then a "0.0034600"
I think the problem has to do with the "strk" command, I want to go to the next value if there is a space. So I wrote...
ptr = strtok(NULL," nr");

<pre class="prettyprint
int Read_RTS(vector<vector<double>> &v,string file_loc,string file_nm)
{
string x;
x = file_loc + file_nm;
FILE *infile;
errno_t e = fopen_s( &infile, x.c_str(), "r" );
if(infile == NULL)
{ LogFile<<"Error - unsuccessful open"<<file_loc<<", "<<file_nm<<endl; exit(0);}
int count = 0;
int i = 0;
char chrX[5000];
char chrX2[5000];
int xyz = 0;
int j = 0;
int p = 0;
string x1;
string xy;
string xy1,xy2,xy3;
int y1=0,y2=0,y3=0,y4=0;
for(i=1;i<=22;i++)//Skip past the headers to the 23rd Row
fgets(chrX,sizeof(chrX)-1,infile);
char *ptr3 = strtok(chrX," nr");
start:
while(fgets(chrX,sizeof(chrX)-1,infile))
{
char *ptr3 = strtok(chrX," nr");
xy = ptr3;
if(xy =="Scenario")
cout<<"Scenario";
if(j==1079)
return 0; //Temporary to get out just fix this later!!
j++;
char *ptr = strtok(chrX," nr");
count = 0; //Reset Column Count
while(ptr)
{
++count;
switch(count)
{
case 14:
xy = ptr;
RTS::RTS_MAP[p][1] = atof(xy.c_str());
break;
case 16:
xy = ptr;
RTS::RTS_MAP[p][2] = atof(xy.c_str());
break;
case 18:
xy = ptr;
RTS::RTS_MAP[p][3] = atof(xy.c_str());
break;
case 20:
xy = ptr;
RTS::RTS_MAP[p][4] = atof(xy.c_str());
break;
case 22:
xy = ptr;
RTS::RTS_MAP[p][5] = atof(xy.c_str());
break;
case 24:
xy = ptr;
RTS::RTS_MAP[p][6] = atof(xy.c_str());
break;
case 26:
xy = ptr;
RTS::RTS_MAP[p][7] = atof(xy.c_str());
break;
case 28:
xy = ptr;
RTS::RTS_MAP[p][8] = atof(xy.c_str());
break;
case 30:
xy = ptr;
RTS::RTS_MAP[p][9] = atof(xy.c_str());
break;

} //End Switch
ptr = strtok(NULL," nr");
}
}//End While Loop
fclose(infile);
return 0;
return 0;
}[/code]
<br/>
<br/>

View the full article
 
Back
Top