我正在尝试从文件中读取保存的数据。我正在尝试将缓冲区中的信息从字符串转换为字符,并将它们的值分配给结构的成员。似乎数据没有被正确访问,当我打印出节点值时,它与文件中存储的不同。我看不出哪里出错了。
文件格式:
3 2 43 4
2 4 5 6
$Node
4
1 0 -1 0
2 0 1 0
3 10 -1 0
4 10 1 0
$EndNodes
$Elements
2
1 2 3 4
2 3 5 6
$EndElements
编码:
struct Node {
int x; // position
int y; //position
int z; //position
int total_node_nums; // total node numbers
};
struct Element{
int total_elmt_num; // total element numbers
struct Node *node; // array of nodes
};
来自 main 的调用:
void arr_creator(char *fname, char *str_start, char *str_end);
arr_creator:
void arr_creator(char *fname, char *str_start, char *str_end){
FILE *fl_read;
char buffer[512], line_buff[20];
int i,num_node,tag,line = 0;
fl_read = fopen(fname, "r");
if(fl_read == NULL){
printf("\n[error reading file] :: in function arr_creator\n");
return;
}
// Scan file for str_start
while(fgets(buffer, sizeof(buffer),fl_read) != NULL ) {
if(strstr(buffer,str_start)){
printf("\nline: %d string: %s \n",line+1, str_start);
fgets(buffer,sizeof(buffer),fl_read);
num_node = atoi(buffer);
printf("num_node = %d\n", num_node);
struct Node *node;
node = malloc((num_node+1)*sizeof(node));
for(i=1; i<num_node+1;i++){
sscanf(buffer,"%d %d %d %d", &tag, &node[i].x, &node[i].y, &node[i].z);
printf("--------------\n");
printf(" Node %d \n", tag);
printf("--------------\n");
printf("node[%d].x = %d\n",i, node[i].x);
printf("node[%d].y = %d\n",i, node[i].y);
printf("node[%d].z = %d\n",i, node[i].z);
printf("BUFFER :: %s", buffer);
}
}
line++;
if(strstr(buffer,str_end)){
printf("buffer at break :: %s", buffer);
// buffer[0] = '\0';
break;
}
}
if(fl_read){
fclose(fl_read);
}
}