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

C 使用fgets()从文件复制到字符串,c,string,file,eof,fgets,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 . . . . .

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

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 . . . . . . . . 示例文本文件如下所示:

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 918 ATH SKG 2011-11-02 20:00 2011-11-02 20:55 0021901 SKG ATH 2011-11-03 05:00 2011-11-03 05:55 0022518-2011-11-02 20:00 2011-11-02 20:50 0022501她的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 0023326 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 0024302 TLV路径2011-11-03 04:00 2011-11-03 06:10 0025530雅典时间2011-11-01 03:50 2011-11-01 04:40 0025531 CHQ ATH 2011-11-01 05:20 2011-11-01 06:10 002675 ATH SKG 2011-11-01 07:05 2011-11-01 08:00 0026175 SKG MUC 2011-11-01 08:40 2011-11-01 10:45 0026176 MUC SKG 2011-11-01 11:35 2011-11-01 13:35 0026176 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 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 尝试一下:

#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;
}
#包括
#包括
#包括
空模(常量字符*消息){
fprintf(标准,“错误:%s.\n”,消息);
退出(退出失败);
}
int main(){
字符行[256];
文件*fp=fopen(“test.txt”、“r”);
如果(fp==NULL)死亡(“无法打开文件”);
//计算唯一的第一个值的数量。
整数计数=0;
char sp[5],sp_last[5]={0};
而(fgets(生产线、生产线尺寸、fp)){
sscanf(直线,“%4s”,sp);
如果(strcmp(sp,sp_last)!=0)
++计数;
strcpy(sp_last,sp);
}
倒带(fp);
char(*sip)[5]=malloc(count*sizeof(*sip));
字符(*std)[17]=malloc(计数*大小(*std));
char(*etd)[17]=malloc(count*sizeof(*etd));
[17]中的char etd_;
if(fgets(行,行的大小,fp)=NULL)
死(“看不懂第一行”);
对于(int i=0;i
我可以建议(至少)这里有一个问题:

char**sip=malloc(count*sizeof(char*));//用于存储航班组合的阵列

对于(i=0;i行总是成对出现吗?是的,最多2、4或6对..当我说成对时,我指的是aaaa、bbbb、cccc等对,它们实际上是像0021、0022 aso这样的文本。那么,在这组具有相同第一个值的2、4、6行中,你到底想提取什么?这是第一行和第二行的第一个日期吗组最后一行的最后一个日期?是的,我想提取
aaaa
然后是第一个[日期和时间]
11..11
然后是最后一个[日期和时间]
44..44
。然后
bbbb
等等…哇,非常感谢你的回答!但它没有按我希望的那样工作。我将用一个示例文本文件更新初始帖子。您编写的内容正确地打印了
sip
字符串数组,但随后它错误地打印了其他值…检查初始帖子以获得预期的answ呃,再次感谢!我不知道您的日期字段中有空格。已修复。(请告诉我,我不仅仅是为您做家庭作业!)感谢更新。不,我正在学习C,我还不完全了解整个fopen、fgets、fgetc的工作原理:)这就是为什么我在玩arround游戏,试图完全理解它们的工作原理…阅读fopen、fgets、fgets等的文档,不要在(!feof…
好的,我将修改:)时执行
,我读过,但你知道,如果你不练习,你自己无法完全理解…哦,谢谢你注意到,我复制粘贴错误..我的不好,无论如何谢谢!
#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;
}
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);