EDN Admin
Well-known member
Hi,<br/>
<br/>
I have a function that reads a text file:
<pre>int Read_xInforce_pointer(void)
{
int x, ab_version;
char chrX[1000];
int ret=0;
int i=1;
FILE * infile;
char temp_name[1000];
infile = fopen("C:tempname.csv", "r");
if(infile == NULL)
{ LogFile<<"Error - unsuccessful open of file xGmabVerTbl.csv"<<endl; exit(0);}
else
LogFile<<"Successfully opened file xGmabVerTbl.csv"<<endl;
int a;
double b;
int x1,x2,x3,x4;
char text[100];
//string text; //This doesnt work
int count;
fgets(chrX,sizeof(chrX)-1,infile); //Skip past header
while(fgets(chrX,sizeof(chrX)-1,infile))
{
//puts(chrX);
count = 0;
char *ptr = strtok(chrX,",");
while(ptr)
{
//puts(ptr);
++count;
switch(count)
{
case 1:
sscanf(ptr,"%d",&x1); //VERSION
break;
case 2:
strcpy(text,ptr); //TEXT NAME
break;
case 3:
sscanf(ptr,"%lf",&x3); //
break;
case 4:
sscanf(ptr,"%d",&x4); //
break;
} //End Switch
ptr = strtok(NULL,",");
} //End Line
}//End File
fclose(infile);
return 0;
}
[/code]
The problem is that if the data has a blank like
1,Name,4.5,3
vs.
1,Name,,3
The reading skips to the next item "3", so x3 gets the value of "3".
I want x3 = 0, and x4 = 3. But it skips to the next item, because it is blank.
How do I fix this?
THanks
View the full article
<br/>
I have a function that reads a text file:
<pre>int Read_xInforce_pointer(void)
{
int x, ab_version;
char chrX[1000];
int ret=0;
int i=1;
FILE * infile;
char temp_name[1000];
infile = fopen("C:tempname.csv", "r");
if(infile == NULL)
{ LogFile<<"Error - unsuccessful open of file xGmabVerTbl.csv"<<endl; exit(0);}
else
LogFile<<"Successfully opened file xGmabVerTbl.csv"<<endl;
int a;
double b;
int x1,x2,x3,x4;
char text[100];
//string text; //This doesnt work
int count;
fgets(chrX,sizeof(chrX)-1,infile); //Skip past header
while(fgets(chrX,sizeof(chrX)-1,infile))
{
//puts(chrX);
count = 0;
char *ptr = strtok(chrX,",");
while(ptr)
{
//puts(ptr);
++count;
switch(count)
{
case 1:
sscanf(ptr,"%d",&x1); //VERSION
break;
case 2:
strcpy(text,ptr); //TEXT NAME
break;
case 3:
sscanf(ptr,"%lf",&x3); //
break;
case 4:
sscanf(ptr,"%d",&x4); //
break;
} //End Switch
ptr = strtok(NULL,",");
} //End Line
}//End File
fclose(infile);
return 0;
}
[/code]
The problem is that if the data has a blank like
1,Name,4.5,3
vs.
1,Name,,3
The reading skips to the next item "3", so x3 gets the value of "3".
I want x3 = 0, and x4 = 3. But it skips to the next item, because it is blank.
How do I fix this?
THanks
View the full article