Segfault使用fgets()从文件复制到字符串

时间:2014-04-25 00:10:17

标签: c string file eof fgets

我有一个文本文件,其中包含日期,时间,小时,日期,如下所示:

aaaa rrr rrr rrr 1111111111111111 2222222222222222
aaaa rrr rrr rrr 3333333333333333 4444444444444444
bbbb rrr rrr rrr 5555555555555555 6666666666666666
bbbb rrr rrr rrr 6666666666666666 7777777777777777
cccc . . . . .
.
.
.

aaaabbbb0001 0002等等,rrr行无关紧要11...11,{{ 1}}等是日期和时间,即22...22

所以,我有3个malloc' ed字符串数组:

  • 啜饮2005-11-03 04:50aaaa
  • 标准为bbbb11..111等(第一行)
  • etd for 33..3322..222等(第二行)

我想要做的是存储:

  • 所有不同的aaaa,bbbb in sip [number_of_them]
  • std [number_of_them]
  • 中的44..44
  • 11...11 in etd [number of them]

注意:例如 - > {1}中的44...44,std [0]中的aaaa和etd [0]中的11...11

每次44...44更改为aaaa,依此类推 - > bbbb啜[1],bbbb到std [1]和55...55到etd [1]

不幸的是,我的代码中出现了一些错误:

77...77

示例文本文件如下所示:

0021 918 ATH SKG 2011-11-02 20:00 2011-11-02 20:55
0021 901 SKG ATH 2011-11-03 05:00 2011-11-03 05:55
0022 518 ATH HER 2011-11-02 20:00 2011-11-02 20:50
0022 501 HER ATH 2011-11-03 05:00 2011-11-03 05:50
0023 325 ATH CAI 2011-11-02 22:50 2011-11-03 00:45
0023 326 CAI ATH 2011-11-03 01:45 2011-11-03 03:45
0024 301 ATH TLV 2011-11-02 23:15 2011-11-03 01:10
0024 302 TLV ATH 2011-11-03 04:00 2011-11-03 06:10
0025 530 ATH CHQ 2011-11-01 03:50 2011-11-01 04:40
0025 531 CHQ ATH 2011-11-01 05:20 2011-11-01 06:10
0026 175 ATH SKG 2011-11-01 07:05 2011-11-01 08:00
0026 175 SKG MUC 2011-11-01 08:40 2011-11-01 10:45
0026 176 MUC SKG 2011-11-01 11:35 2011-11-01 13:35
0026 176 SKG ATH 2011-11-01 14:15 2011-11-01 15:10

预期的答案是:

0021 2011-11-02 20:00 2011-11-03 05:55
0022 2011-11-02 20:00 2011-11-03 05:50
0023 2011-11-02 22:50 2011-11-03 03:45
0024 2011-11-02 23:15 2011-11-03 06:10
0025 2011-11-01 04:40 2011-11-01 06:10
0026 2011-11-01 07:05 2011-11-01 15:10

2 个答案:

答案 0 :(得分:1)

尝试一下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void die(const char *msg) {
    fprintf(stderr, "Error: %s.\n", msg);
    exit(EXIT_FAILURE);
}

int main() {
    char line[256];

    FILE *fp = fopen("test.txt", "r");
    if (fp == NULL) die("Can't open file");

    // Count the number of unique first values.
    int count = 0;
    char sp[5], sp_last[5] = {0};
    while (fgets(line, sizeof line, fp)) {
        sscanf(line, "%4s", sp);
        if (strcmp(sp, sp_last) != 0)
          ++count;
        strcpy(sp_last, sp);
    }
    rewind(fp);

    char (*sip)[5]  = malloc(count * sizeof(*sip));
    char (*std)[17] = malloc(count * sizeof(*std));
    char (*etd)[17] = malloc(count * sizeof(*etd));
    char etd_in[17];

    if (fgets(line, sizeof line, fp) == NULL)
        die("Can't read first line");

    for (int i = 0; i < count; ++i) {
        if (sscanf(line, "%4s %*s %*s %*s %16c", sip[i], std[i]) != 2)
            die("Can't scan line (a)");
        std[i][16] = '\0';
        while (fgets(line, sizeof line, fp)) {
            if (sscanf(line, "%4s %*s %*s %*s %*16c %16c", sp, etd_in) != 2)
                die("Can't scan line (b)");
            etd_in[16] = '\0';
            if (strcmp(sp, sip[i]) == 0)
                strcpy(etd[i], etd_in);
            else
                break;
        }
    }

    for (int i = 0; i < count; ++i)
        printf("%s %s %s\n", sip[i], std[i], etd[i]);

    free(sip);
    free(std);
    free(etd);

    return 0;
}

答案 1 :(得分:1)

我可以建议一个问题(至少)在这里:

char** sip = malloc(count*sizeof(char*));            // Array for storing flight combinations
for (i=0; i<count; i++) sip[i] = malloc(5);

char** std = malloc(count*sizeof(char*));            // Array for storing starting time and date
for (i=0; i<count; i++) sip[i] = malloc(17);

char** etd = malloc(count*sizeof(char*));            // Array for storing ending time and date
for (i=0; i<count; i++) sip[i] = malloc(17);

您分配了三个字符串数组sipstdetd,但之后只初始化sip的元素,每个元素三次。第二个for循环可能应初始化std,第三个应初始化etd