c - 解析文本文件时出现段错误

标签 c linux

我正在做一些简单的逻辑来解析逗号分隔的文本文件。当我给出 sz+1 时,我恰好在最外层的 while 循环中出现段错误。但是我使用了 sz+1 而不是 sizeof(str1) 然后我顺利地摆脱了它但是结果没有准确捕获。完成总迭代后,我在最后收到段错误核心转储错误。 请任何人帮助我...

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

最佳答案

使用调试器。添加一些 #include 并将 SUCCESS 更改为 EXIT_SUCCESS 后,GDB 给出了以下内容:

(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);

现在,您可以使用调试器检查您的变量。让我们看看ptr:

(gdb) p ptr
$1 = 0x0

NULL 上调用 strlen 当然是不允许的。查看实际的程序源码,可以看到在使用ptr之前没有初始化ptr(而且在本例中,它刚好得到的初始值是0)。

关于c - 解析文本文件时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7957385/

相关文章:

c++ - OCI 在不知道表结构的情况下获取数据

c - install_man() 不获取生成文件的路径

c - 图是否连通!福特富尔克森的C语言

c - Linux 中 C 语言程序的一些选项

php - 如何将 Gimp 预加载到网络服务器的内存中?

php - URL 的 fopen() 中断域名,而不是数字地址

c - 添加到链表时释放临时节点

c - Raspberry 微内核,似乎什么都不做

python - 在 python 3.1 中读取数据文件

linux - 在 linux 中使用 sed 将一个文件的特定列替换为另一个文件的列