Reputation: 85
I am doing some simple logic to parse a comma separated text file. I am getting segmentation error at exactly in the most outer while loop as i am giving sz+1. but instead sz+1 i have used sizeof(str1) then i am getting out of it smoothly but the results are not capturing accurately. I am getting segmentation fault core dumped error at the end after completing total iterations.. please any one help me...
int main()
{
char *str1;
unsigned char user_id[10]="",pwd[10]="",name[75]="",frm_dt[15]="", to_dt[15]="", ntry_dt[15]="",lim_amt[15]="",act_flag[5]="";
char *ptr;
char *temp;
int i=1,t,rc=0,sz=0;
char delims[]=",";
char filename[100] = "/home/erpdirect/Desktop/billcollectors.txt";
FILE *fp;
temp = (char*)malloc(strlen(ptr)+1);
rc=0;
fp= fopen("/home/erpdirect/Desktop/billcollectors.txt","r");
if (fp == NULL)
{
printf("No such file");
//return 1;
}
fseek(fp, 0L, SEEK_END);
sz = ftell(fp);
fseek(fp, 0L, SEEK_SET);
printf("Size of File : %d \n",sz);
printf("\nLoop is in file pointer\n");
str1 =(char*)malloc(sz*sizeof(char));
while(fgets(str1,sz+1,fp) !=NULL)
{
printf("\n");
ptr=strtok(str1,delims);
//printf("\nPoints to >>>>>>>>> %s",ptr);
while(ptr != NULL)
{
i=1;
memset(user_id,0,sizeof(user_id));
memset(name,0,sizeof(name));
memset(pwd,0,sizeof(pwd));
memset(frm_dt,0,sizeof(frm_dt));
memset(to_dt,0,sizeof(to_dt));
memset(lim_amt,0,sizeof(lim_amt));
memset(ntry_dt,0,sizeof(ntry_dt));
memset(act_flag,0,sizeof(act_flag));
while(ptr!=NULL && i<=16)
{
strcpy(temp, ptr);
printf("\nTemp values are : %s",temp);
ptr = strtok(NULL,delims);
//printf("\nPoints to >>>>>>>>> %s",ptr);
switch(i)
{
case 1: strcpy(user_id,temp);
printf("\nUSER ID: %s",user_id);
break;//insert into categoryId
case 2: strcpy(pwd,temp);
printf("\nPASSWORD: %s",pwd); break;
case 3: strcpy(name,temp);
printf("\nName: %s",name); break;
case 4: strcpy(frm_dt,temp);
printf("\nFROM DATE: %s",frm_dt); break;
case 5: strcpy(to_dt,temp);
printf("\nTO DATE: %s",to_dt); break;
case 6: strcpy(lim_amt,temp);
printf("\nLIMIT Amt: %s",lim_amt); break;
case 7: strcpy(ntry_dt,temp);
printf("\nEntry Date: %s",ntry_dt); break;
case 8: strcpy(act_flag,temp);
printf("\nActive flag: %s",act_flag); break;
default:break;
}//switch
i++;
}//while
}//while
}//while
free(str1);
free(temp);
fclose(fp);
return SUCCESS;
}
Upvotes: 0
Views: 854
Reputation: 51197
Use a debugger. After adding in some #include
s, and changing SUCCESS
to EXIT_SUCCESS
, here is what GDB gives:
(gdb) run
Starting program: /tmp/test
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400a00 in main () at test.c:14
14 temp = (char*)malloc(strlen(ptr)+1);
Now, you can use the debugger to inspect your variables. Let's look at ptr
:
(gdb) p ptr
$1 = 0x0
Calling strlen
on NULL
is, of course, not allowed. When you look at the actual program source, you can see that you failed to initialize ptr before using it (and in this case, the initial value it just happened to get was 0).
Upvotes: 2
Reputation: 43558
At the time you call strlen(ptr)
, the ptr
pointer is uninitialised, and you invoke undefined behaviour:
/* there is random garbage in ptr, instead of a
* valid address to a nul-terminated string
*/
temp = (char*) malloc(strlen(ptr)+1);
Also note that casting void pointers on the right side of an assignment (or particularly the result of malloc
) is unnecessary in C, unlike in C++.
Upvotes: 1