gpt4 book ai didi

c - 正确读取文本文件

转载 作者:行者123 更新时间:2023-11-30 15:46:39 24 4
gpt4 key购买 nike

我应该读取以这种方式格式化的文件并将信息存储到结构中:

Johnny Rico A B A B C C C B B A
Ace Levy A A B A C A C A C A
Carmen Ibanez A B B B C C B B B B
Dizzy Flores B A A B C C C B B A
Zander Barcalow A B A B C C A B B A
Carl Jenkins A A A C B C A B B B

编辑

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

#define N 100

typedef struct _cad
{
char nome[36], cognome[36];
char risultati[50];
} Cadetto;

/*
*
*/
int main(int argc, char** argv)
{
FILE *fp = NULL;
int j = 0, count = 0, i, k;
char riga[255];
Cadetto cadetti[100];
char *ptk;

if(argc != 2)
{
fprintf(stderr, "Error: no parameter has been passed!\n");
exit(1);
}


if((fp = fopen(argv[1], "r")) == NULL)
{
fprintf(stderr, "Error opening file %s\n", argv[1]);
exit(1);
}

while (fgets(riga, sizeof(riga), fp) != NULL)
{
j = 0;

ptk = strtok(riga, " ");
strcpy(cadetti[count].nome, ptk);

ptk = strtok(NULL, " ");
strcpy(cadetti[count].cognome, ptk);

while (ptk != NULL)
{
ptk = strtok(NULL, " ");
cadetti[count].risultati[j] = *ptk;
j++;
}

count++;
}

fclose(fp);

for (i = 0; i < count; i++)
{
printf("%s %s", cadetti[i].nome, cadetti[i].cognome);

for (k = 0; k < j; k++)
{
printf(" %c", cadetti[i].risultati[k]);
}

printf("\n");
}

return (EXIT_SUCCESS);
}

这是程序,但它不再工作

enter image description here

最佳答案

这段代码对我有用:

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

#define N 100

typedef struct _cad
{
char nome[36], cognome[36];
char risultati[50];
int nrisultati;
} Cadetto;

int main(int argc, char** argv)
{
FILE *fp = NULL;
int i, k;
int count;
char riga[255];
Cadetto cadetti[N];
char *ptk;

if(argc != 2)
{
fprintf(stderr, "Error: no parameter has been passed!\n");
exit(1);
}

if((fp = fopen(argv[1], "r")) == NULL)
{
fprintf(stderr, "Error opening file %s\n", argv[1]);
exit(1);
}

for (count = 0; count < N && fgets(riga, sizeof(riga), fp) != NULL; count++)
{
int j = 0;
printf("Input: %s", riga);

ptk = strtok(riga, " ");
strcpy(cadetti[count].nome, ptk);

ptk = strtok(NULL, " ");
strcpy(cadetti[count].cognome, ptk);
printf("Name: %s %s\n", cadetti[count].nome, cadetti[count].cognome);

while ((ptk = strtok(NULL, " ")) != NULL)
{
cadetti[count].risultati[j] = *ptk;
printf("Grade %d: %c\n", j, *ptk);
j++;
}
cadetti[count].nrisultati = j;
}

fclose(fp);

for (i = 0; i < count; i++)
{
printf("%s %s", cadetti[i].nome, cadetti[i].cognome);

for (k = 0; k < cadetti[i].nrisultati; k++)
{
printf(" %c", cadetti[i].risultati[k]);
}

printf("\n");
}

return (EXIT_SUCCESS);
}

nrisultati 成员单独跟踪每个Cadetto 的成绩数量,以防数据不一致。主读取循环受到保护,不会溢出数组。我没有保护名称读取代码免于访问带有格式错误的数据(例如单个单词)的空指针。如果名称也过长,可能会出现问题。

主要变化在于等级扫描循环; strtok() 的结果在使用之前会被检查。

给定数据的示例输出:

Input: Johnny Rico A B A B C C C B B A
Name: Johnny Rico
Grade 0: A
Grade 1: B
Grade 2: A
Grade 3: B
Grade 4: C
Grade 5: C
Grade 6: C
Grade 7: B
Grade 8: B
Grade 9: A
Input: Ace Levy A A B A C A C A C A
Name: Ace Levy
Grade 0: A
Grade 1: A
Grade 2: B
Grade 3: A
Grade 4: C
Grade 5: A
Grade 6: C
Grade 7: A
Grade 8: C
Grade 9: A
Input: Carmen Ibanez A B B B C C B B B B
Name: Carmen Ibanez
Grade 0: A
Grade 1: B
Grade 2: B
Grade 3: B
Grade 4: C
Grade 5: C
Grade 6: B
Grade 7: B
Grade 8: B
Grade 9: B
Input: Dizzy Flores B A A B C C C B B A
Name: Dizzy Flores
Grade 0: B
Grade 1: A
Grade 2: A
Grade 3: B
Grade 4: C
Grade 5: C
Grade 6: C
Grade 7: B
Grade 8: B
Grade 9: A
Input: Zander Barcalow A B A B C C A B B A
Name: Zander Barcalow
Grade 0: A
Grade 1: B
Grade 2: A
Grade 3: B
Grade 4: C
Grade 5: C
Grade 6: A
Grade 7: B
Grade 8: B
Grade 9: A
Input: Carl Jenkins A A A C B C A B B B
Name: Carl Jenkins
Grade 0: A
Grade 1: A
Grade 2: A
Grade 3: C
Grade 4: B
Grade 5: C
Grade 6: A
Grade 7: B
Grade 8: B
Grade 9: B
Johnny Rico A B A B C C C B B A
Ace Levy A A B A C A C A C A
Carmen Ibanez A B B B C C B B B B
Dizzy Flores B A A B C C C B B A
Zander Barcalow A B A B C C A B B A
Carl Jenkins A A A C B C A B B B

将调试打印注释掉:

Johnny Rico A B A B C C C B B A
Ace Levy A A B A C A C A C A
Carmen Ibanez A B B B C C B B B B
Dizzy Flores B A A B C C C B B A
Zander Barcalow A B A B C C A B B A
Carl Jenkins A A A C B C A B B B

关于c - 正确读取文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18208210/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com