2016-11-14 77 views
-1

我有一個代碼,它將讀取一個csv文件並將數據保存在緩衝區中。但我得到分段錯誤核心轉儲作爲狀態錯誤。c中的分段錯誤(core dump)

的CSV文件的內容是

TimeStamp BlockSeqNum SeqNum X-axis Y-axis Z-axis 
4294967295 0 0 27 20 -4 
4294967295 0 1 48 11 -13 
4294967295 0 2 45 0 -7 
4294967295 0 3 38 -9 -2 
4294967295 0 4 34 -42 -28 
4294967295 0 5 -29 35 -35 
4294967295 0 6 -3 -46 0 
4294967295 0 7 0 2 -10 
4294967295 0 8 6 113 -32 
4294967295 0 9 -4 20 27 
4294967295 0 10 -14 -15 -19 
4294967295 0 11 23 51 -19 
4294967295 0 12 10 34 1 
4294967295 0 13 -7 -2 15 
4294967295 0 14 13 -5 -30 
4294967295 0 15 -24 30 51 
4294967295 0 16 -18 39 -45 

我的代碼讀取和解析該文件是

#include<stdio.h> 
#include<stdlib.h> 



struct pattern_buffer { //structure to store the contents of csv files 
    unsigned int x[1024]; 
    unsigned int y[1024]; 
    unsigned int z[1024]; 

}Data_buffer; 

int main(void) 
{ 
    char buffer[1024]; 

    char *record,*line; 
    int x1=0,y1=0,z1=0,linecount=0; //linecount is used to check weather 1024 rows have been read or not 

    int n=0; 


    FILE *fstream = fopen("files.csv","r"); 
    if(fstream == NULL)    //check file 
    { 
     printf("\n inside .so \n unable to open csv files \n"); 
     return 0; 
    } 
    printf("\n Sensor values are \n"); 

    line=fgets(buffer,sizeof(buffer),fstream); //skip first line (weather it will skip the first line 

    while((line=fgets(buffer,sizeof(buffer),fstream))!=NULL && linecount<1024) 
    { 
     record=strtok(line,","); //skip first column 
     while(record!= NULL) 
     { 
      //record=strtok(NULL,","); //skip second column 
      record=strtok(NULL,",");  //skip third column 
      record=strtok(NULL,",");  //get x axis  
      Data_buffer.x[x1++]=atoi(record); 
      record=strtok(NULL,",");   //get y axis  
      Data_buffer.y[y1++]=atoi(record); 
      //record=strtok(NULL,",");  //get z axis  
      Data_buffer.z[z1++]=atoi(record); 

     } 
     linecount++; 
    } 
    return -1; 
} 

所以,請註明我度過我的邏輯是跳過第一線和讀取正確csv值。

+3

注意:CSV示例中沒有逗號。 '-4'對於'unsigned int'不是一個有效的值。 – wildplasser

+0

其實它並不是csv文件的副本。它有逗號,沒有顯示 –

+0

我已經使用了現在已經簽名的int,但仍然無法出去放,相同的錯誤 –

回答

0

對不起,我找到了這個問題的解決方案。

我沒有包含在頭文件中,所以strtok無法爲我解析令牌。我在調試時檢查了記錄的值,但記錄值爲零。所以當我研究了很多,我才知道我沒有包括哪些是導致我錯誤。