I'm getting some problems when using fscanf to read a file.
This is a piece of the program code:
Expand|Select|Wrap|Line Numbers
- #include <stdlib.h>
- #include <stdio.h>
- int main()
- {
- //variable declaration
- double position[1000][2]={0};
- FILE *fp;int j;
- fp=fopen("1.txt","r");
- char array[100];
- while(!feof(fp))
- {
- fgets(array,100,fp);
- //skip the comment lines which begins with '#'
- if(strncmp(array,"#",1)!=0)
- {
- //read from data files with fscanf
- for(j=1;j<30;j++)
- {
- fscanf(fp,"%lf, %lf, %lf %*[^\n]",&position[j][0],&position[j][1],&position[j][2]);
- }//for(j=0;j<1000;j++)
- }//if(strncmp(array[i],####,1)!=0)
- }//while(!feof(fp))
- fclose(fp);
- //print points out
- for(j=1;j<7;j++)
- {
- printf("Point %d is %lf %lf %lf\n", j,position[j][0],position[j][1],position[j][2]);
- }//for(j=1;j<7;j++)
- }
#DATA
003360073, 003362061, 240 #AP-B_LIE_PP01-523
003360073, 003351154, 244 #AP-B_LIE_PP01-10
003360073, 003351154, 243 #AP-B_LIE_PP01
The output is the following:
Point 1 003360073 003362061 240 AP-B_LIE_PP01-523003360073
Point 2 003360073 003351154 244 AP-B_LIE_PP01-10
Point 3 003360073 003351154 243 AP-B_LIE_PP01
The problem is that when the word ends with a minus sign followed by 3
digits (see first line) he takes directly the following word: so
instead of AP-B_LIE_PP01-523 I receive AP-B_LIE_PP01-523003360073.
Can someone help me on this one?????