我正在尝试执行此处已完成的操作Read co-ordinates from a txt files using C Program。我尝试输入的数据采用以下格式:
f 10 20 21
f 8 15 11
. . . .
f 11 12 25
我的点结构的唯一区别是我有一个额外的字符来存储第一列中的字母(可能是也可能不是字母 f)。我想我要么声明我的字符错误,要么我错误地调用它printf
。无论哪种方式,我只读取第一行,然后我的程序终止。有任何想法吗 ?
下面是我的 MWE
#define FILEPATHtri "/pathto/grid1DT.txt"
#define FILEPATHorg "/pathto/grid1.txt"
#define MAX 4000
#include <stdio.h>
#include <stdlib.h>
#include "math.h"
typedef struct
{
float x;
float y;
float z;
char t[1];
}Point;
int main(void) {
Point *points = malloc( MAX * sizeof (Point) ) ;
FILE *fp ;
fp = fopen( FILEPATHtri,"r");
int i = 0;
while(fscanf(fp, "%s %f %f %f ", points[i].t, &points[i].x, &points[i].y, &points[i].z ) == 4 )
{
i++;
}
fclose(fp);
int n;
for (n=0; n<=i; n++){
printf("%c %2.5f %2.5f %2.5f \n", points[i].t, points[n].x, points[n].y, points[n].z ); }
printf("There are i = %i points in the file \n And I have read n = %i points ",i,n);
return 0;
}