正确读取文本文件

时间:2013-08-13 11:43:11

标签: c file struct

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

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

2 个答案:

答案 0 :(得分:1)

此代码适用于我:

#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

答案 1 :(得分:0)

sscanf(riga, "%s %s %s", cadetti[count].nome,
                                 cadetti[count].cognome,
                                 result);

==&GT;

char * tmp = strtok( riga, " ");
memcpy(cadetti[count].nome,tmp,strlen(tmp)+1);

tmp= strtok (NULL " ");
memcpy(cadetti[count].cognome,tmp,strlen(tmp)+1); 

tmp= strtok(NULL,'\0');
memcpy(result,tmp,strlen(tmp)+1);