2016-05-13 42 views
0

**>有没有办法访问来自其他结构的变量?当如何访问C中的结构中的变量以进行打印?

i try this code,i am getting this compile error. ** test.c: In function ‘readRecordsFromFile’: test.c:70:18: error: expected expression before ‘kdnode’ printf(" %f\n",kdnode.data.latitude);

#include <stdio.h> 
#include <string.h> 
#include <stdbool.h> 
#include <stdlib.h> 
#include <math.h> 
#define rec_size 112 
typedef struct _Node kdnode; 
typedef struct _Record record; 

static void readRecordsFromFile(char *filename); 
struct _Record{ 
    int plateNumber; 
    long *name[32]; 
    double area; 
    int population; 
    int density; 
    int popcitycenter; 
    long region; 
    double latitude; 
    double longtitude; 
}; 
struct _Node 
    { 
     //kdnode left; 
     //kdnode right; 
     record data; 
     bool type; 
     double x; 
     double y; 
     int pagenumber; 
    }; 
    int main(){ 
    readRecordsFromFile("data.dat"); 
     return 0; 
    } 

    static void readRecordsFromFile(char *filename) 
    { 
     FILE* inputFile; 
     inputFile = fopen(filename, "rb"); 
     int i; 
     if(!inputFile) { 
     printf("Could not open file"); 
     return; 
    } 
     int length,record_count; 
     fseek(inputFile,0,SEEK_END); 
     length=ftell(inputFile); 
     fseek(inputFile,0,SEEK_SET); 
     record_count = length/sizeof(record); 
     kdnode kd; 
     fread(&kd,rec_size,2,inputFile); 
     printf("%d",ftell(inputFile)); 
     for (i = 0; i < record_count; i++) 
     { 
     printf(" %f\n",kdnode.data.latitude); 
     } 
      fclose(inputFile); 
    } 
+2

仔细格式化您的代码,以便阅读它是愉快的。 –

回答

4

typedef struct _Nodetypedef编作为knodeknode表示数据类型,它不是一个标识符,所以这

printf(" %f\n",kdnode.data.latitude); 

必须

printf(" %f\n", kd.data.latitude); 

您也应该检查返回值像fread()例如功能。