[英]Parsing data from ASCII formatted file in C
I am trying to do what's been done here Read co-ordinates from a txt files using C Program . 我正在尝试做这里所做的事情使用C程序从txt文件中读取坐标 。 The data that I am trying to input is in this format:
我尝试输入的数据采用以下格式:
f 10 20 21
f 8 15 11
. . . .
f 11 12 25
The only difference in my point structure is that I have a an extra char to store the letter in the first column (which may or may not be the letter f). 我的点结构的唯一区别是我有一个额外的字符来存储第一列中的字母(可能是也可能不是字母f)。 I guess im either declaring my char wrong, or I'm calling it in
printf
incorrectly. 我想我要么声明我的错误,要么我在
printf
错误地调用它。 Either way, I only get the first line read and then my program terminates. 无论哪种方式,我只读取第一行,然后我的程序终止。 Any ideas ?
有任何想法吗 ?
Here is my MWE below 这是我的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;
}
Since there's only 1 char in there, not a string just use a single char in your code: 因为那里只有一个字符,所以不是字符串只需在代码中使用一个字符:
char t;
}Point;
Then when you read it in: 然后,当你阅读它:
while(fscanf(fp, "%c %f %f %f ", &points[i].t, &points[i].x, &points[i].y, &points[i].z ) == 4 )
{
I'll note that having an array of 1 char, at the end of a structure, sets you up for the struct hack which might not have been your intentions... A good reason to use just char t
instead of char t[1]
我会注意到,在一个结构的末尾有一个1个字符的数组,为你设置结构hack ,这可能不是你的意图......使用
char t
而不是char t[1]
一个很好的理由char t[1]
Also this line: 这一行:
for (n=0; n<=i; n++){
Should be 应该
for (n=0; n<i; n++){
One last note... if you wanted to print the character out that you read in the prints at the bottom, you should be using n
: 最后一个注意事项......如果你想打印你在底部的印刷品中读到的字符,你应该使用
n
:
// note your previous code was points[i].t
printf("%c %f %f %f \n", points[n].t, points[n].x, points[n].y, points[n].z ); }
Check this 检查一下
while(fscanf(fp, "%c %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[n].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);
getch();
return 0;
}
modification are since only a single character is read %s
modified to %c
also in printf its not points[i].t
its points[n].t
. 修改是因为只有一个字符被读取
%s
修改为%c
也在printf中它不是points[i].t
它的points[n].t
。 Also the limit checking in for loop is also corrected to n<i
此外,for循环中的限制检查也被校正为
n<i
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.