c中的分段错误(核心转储)
Segmentation fault (core dump) in c
我有一个代码可以读取 csv 文件并将数据保存在缓冲区中。但是我收到分段错误核心转储作为状态错误。
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 值。
抱歉,我已经找到解决此问题的方法。
我没有包含在头文件中,因此 strtok 无法为我解析令牌。我在调试时检查了记录的值,但记录值为零。因此,当我进行大量研究后,我才知道我没有包括在内,这是导致我出错的原因。
我有一个代码可以读取 csv 文件并将数据保存在缓冲区中。但是我收到分段错误核心转储作为状态错误。
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 值。
抱歉,我已经找到解决此问题的方法。
我没有包含在头文件中,因此 strtok 无法为我解析令牌。我在调试时检查了记录的值,但记录值为零。因此,当我进行大量研究后,我才知道我没有包括在内,这是导致我出错的原因。