我应该读取以这种方式格式化的文件并将信息存储到一个结构中:
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);
}
这是程序,但它不再工作