2016-05-13 26 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()例如功能。