Question: C PROGRAMMING I get a segmentation fault wen trying to run the program. When I debug it using gdb I get I get the error:
C PROGRAMMING
I get a segmentation fault wen trying to run the program.
When I debug it using gdb I get I get the error:
Starting program: /home/s231453/Oblig_1/a.out
Program received signal SIGSEGV, Segmentation fault.
__strlen_ia32 () at ../sysdeps/i386/i586/strlen.S:94
94 ../sysdeps/i386/i586/strlen.S: No such file or file directory.
And I have no idea where the problem is, can anyone help me?
PS: It says that the error happened in line 94, but I have taken a few lines away when I pasted it in here. So, you will have to debug it again and see where the error is now.
Thank you so much for help.
CODE:
#include
#include
#include
#include
#define ARRAY_LENGTH 250
typedef struct medlemsinfo// main structure
{
char name_1 [250];
char name_2 [100];
char name_3 [100];
int pub_key;
}student_t;
/********************************************
******* function to read student data *******
********************************************/
student_t les_data (char *klasseliste);
/********************************************
******* function to print student data ******
********************************************/
void skriv_data(student_t pStudent, int index);
student_t les_data (char *str)// reading records
{
student_t s1;
int counter = 0;
strcpy(s1.name_3, " ");
char *token = strtok(str, " ");
while( token != NULL )
{
switch(counter){
case 0:
strcpy(s1.name_1, token);
break;
case 1:
strcpy(s1.name_2, token);
break;
default:
if(isdigit(token[0])) {
s1.pub_key = atoi(token);
}
else {
strcat(s1.name_3, " ");
strcat(s1.name_3, token);
}
break;
}
counter++;
token = strtok( NULL, " " );// reset the token flag
}
return s1;
}
//----------------------------------------//
void skriv_data(student_t pStudent, int index)
{
if (index == 1 ){
printf("%s\t%s\t%s\t%d ", pStudent.name_1, pStudent.name_2 ,pStudent.name_3,pStudent.pub_key);
}
else if (index == 2 ){
printf("%d\t%s\t%s\t%s ", pStudent.pub_key, pStudent.name_1 ,pStudent.name_2,pStudent.name_3);
}
else if (index == 3){
printf("%s\t%s\t%s\t%d\tn", pStudent.name_2 ,pStudent.name_1 ,pStudent.name_3,pStudent.pub_key);
}
}
int main (int argc, char *argv[])
{
/** first check number of arguments **/
int input_index;
char *tmp;
setenv("miljo",argv[1],1);// create and read value for enviornment variable
tmp = getenv("miljo");
if(strcmp(tmp,"1"))
{
input_index = 1;
}
else if (strcmp(tmp,"2"))
{
input_index = 2 ;
}
else if (strcmp(tmp,"3"))
{
input_index = 3 ;
}
student_t s_arr[100];
int ii ; // to loop through the list
FILE* file;
file = popen("./klasselist.sh","r");
if (!file){
return -1;
}
/** create a string to buffer the names **/
char str[ARRAY_LENGTH];
int s_arr_index = 0;
while (fgets(str, ARRAY_LENGTH, file))
{
s_arr[s_arr_index] = les_data(str);
s_arr_index++;
}
for(ii = 0 ; ii< s_arr_index ; ii++){
skriv_data(s_arr[ii],input_index);
}
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
