Akshay Vijapur Akshay Vijapur - 17 days ago 6
C Question

Segmentation fault (core dump) in c

I have a code which will read a csv file and save the data in the buffer. But i am getting segmentation fault core dump as the status error.

The contents of csv files are

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


My code to read the and parse the file is

#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;
}


So please specify me weather my logic is correct for skipping first line and reading the csv values .

Answer

Sorry , i have found the solution for this problem .

i have not included in the header file so strtok was not able to parse the token for me. i have checked the value of record while debugging but the record value was zero . so when i researched a lot i came to know that i have not included which was causing me error .

Comments