简体   繁体   English

Strtok和fgets目标C函数导致读取错误以及分段错误

[英]Strtok and fgets Objective C Functions are causing read errors along with segmentation faults

Below is the code I currently have to read in data from a file. 以下是我目前必须从文件中读取数据的代码。 When I comment out the while loop and try to just read in one line of the data I get a few null or 0 values printed followed by the rest of the data in a block and a segmentation fault message at the end. 当我注释掉while循环并尝试仅读入一行数据时,我得到了一些null或0值打印,然后将其余数据放在一个块中,最后出现分段错误消息。 When I leave the while loop I get nothing but the segmentation fault message. 当我退出while循环时,除了分段错误消息外什么都没有。 However, if I choose to just read in the first variable without the loop everything works fine. 但是,如果我选择仅读取第一个变量而不进行循环,则一切正常。 Below I have included my code and the test file. 下面,我包括了我的代码和测试文件。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BUF_LEN 1024

int main(){

FILE *file;
int i;
char *SywUserId, *IntractnId, *DivNbr, *ItemNbr, *KsnId, *buddy_cnt, *cnt_rank,      *table_type, *summer_active, *winter_active;
char buf[BUF_LEN];

file = fopen( "ea_test_rec.txt" , "r");

fgets(buf, BUF_LEN, file); 
while(!feof(file)) {
    if(strlen(buf) < 2) break;
    i=strlen(buf)-1; while(i > 0 && buf[i] <= ' ') buf[i--] = '\0';
    SywUserId = strtok(buf, ",");
    table_type = strtok(NULL, ",");
    cnt_rank = strtok(NULL, ",");
    IntractnId = strtok(NULL, ",");
    DivNbr = strtok(NULL, ",");
    ItemNbr = strtok(NULL, ",");
    KsnId = strtok(NULL, ",");
    buddy_cnt = strtok(NULL, ",");
    summer_active = strtok(NULL, ",");
    winter_active = strtok(NULL, ",");

    printf("%s:%s:%s:%s:%s:%s:%s:%s:%s:%s\n", (SywUserId, table_type, cnt_rank, IntractnId, DivNbr, ItemNbr, KsnId, buddy_cnt, summer_active, winter_active));
            fgets(buf, BUF_LEN, file);
}
    fclose(file);
}

ea_test_rec.txt ea_test_rec.txt

1,1,1,-276551,7,63062,0,1.993,0,0,
1,1,10,24315147,54,41796,0,1.934,0,0,
1,1,11,25562371,2,40396,3747849,1.934,0,0,
1,1,12,-948793,2,2820,0,1.919,0,0,
1,1,13,4272725,44,20243,0,1.911,0,0,
1,1,14,2917566,44,71641,0,1.900,0,0,
1,1,15,24655338,54,71593,0,1.898,0,0,
1,1,16,22365342,44,67862,0,1.894,0,0,
1,1,17,12690269,44,14216,0,1.886,0,0,
1,1,18,2920093,44,93074,0,1.875,0,0,
1,1,19,8569801,2,40396,0,1.868,0,0,
1,1,2,-273684,7,63204,0,1.984,0,0,
1,1,20,10171246,88,2379,0,1.859,0,0,
1,1,3,1617035,44,72854,0,1.977,0,0,
1,1,4,12690127,44,14602,0,1.973,0,0,
1,1,5,13064870,44,13666,0,1.966,0,0,
1,1,6,1616493,44,34869,0,1.966,0,0,
1,1,7,1617032,44,72854,0,1.956,0,0,
1,1,8,1616460,44,23337,0,1.950,0,0,
1,1,9,24655107,2,94350,0,1.948,0,0,

The contents of your strtok buffers is probably not what you expected. 您的strtok缓冲区的内容可能不是您期望的。 I would definitely take @Joachim's advice regarding the debugger. 我肯定会接受@Joachim关于调试器的建议。 Debugger's are a great friend during dev time. 在开发期间,调试器是一个很好的朋友。

Following is an image of the string results from strtok()'ing first line buffer: 以下是strtok()的第一行缓冲区产生的字符串图像:

在此处输入图片说明

Remove the unnecessary () from your printf statement... ie change your printf statement from: 从printf语句中删除不必要的() ,即从以下位置更改printf语句:

printf("%s:%s:%s:%s:%s:%s:%s:%s:%s:%s\n", (SywUserId, table_type, cnt_rank, IntractnId, DivNbr, ItemNbr, KsnId, buddy_cnt, summer_active, winter_active));

To: 至:

printf("%s:%s:%s:%s:%s:%s:%s:%s:%s:%s\n", SywUserId, table_type, cnt_rank, IntractnId, DivNbr, ItemNbr, KsnId, buddy_cnt, summer_active, winter_active);

Output after making these changes: 进行以下更改后的输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM