私はここで行われたことをしようとしています C プログラムを使用して txt ファイルから座標を読み取ります。入力しようとしているデータは次の形式です。
f 10 20 21
f 8 15 11
. . . .
f 11 12 25
私のポイント構造の唯一の違いは、最初の列に文字を格納するための追加の文字があることです (文字 f である場合とそうでない場合があります)。私は自分のcharを間違って宣言しているか、間違って呼び出していると思い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;
}