簡體   English   中英

如何從 txt 文件(僅國家/地區名稱)中掃描並將它們存儲在二維數組中?

[英]How do I scan from a txt file (only the country names) and store them in a 2D array?

Australia   67      57      54   
England     57      66      53   
Canada      26      32      34   
India       22      16      23   
NewZealand  20      12      17   
Scotland    13      11      27   
Nigeria     12      9       14   
Wales       8       6       14   
SAfrica     7       9       11    
Uganda      3       0       2
Samoa       1       4       0
BVIslands   2       0       0
Ghana       0       2       3
Namibia     0       0       4
Dominica    0       2       0

以上是文本文件中的文本。

這是我的代碼:

#define COUNTRIES 15
#define MEDALCAT 3
#define MAX_LENGTH_CNAME 100

void
readFromFile(char fileWithMedals[30], int country[COUNTRIES][MEDALCAT],
    char countryNames[COUNTRIES][MAX_LENGTH_CNAME])
{

    FILE *cWealth = fopen("commonWealth.txt", "r");
    char single = fgetc(cWealth);

    if (cWealth == NULL) {
        printf("Error! File was not created!");
        exit(1);
    }
    int line = 0;

    while (!feof(cWealth) && !ferror(cWealth)) {
        if (fgets(countryNames[line], MAX_LENGTH_CNAME, cWealth) != NULL) {
            line++;
        }
    }

    fclose(cWealth);

    for (int i = 0; i < line; i++) {
        printf("%s", countryNames[i]);
    }

    fclose(cWealth);
}

我需要以某種方式只讀取和存儲國家名稱。 並分別存儲數字的第1、2、3列。

幾個問題...

  1. char single = fgetc(cWealth); 應該if (cWealth == NULL)塊之后完成。
  2. 它沒有任何作用,會被編譯器標記為-Wall
  3. 如上所述,使用feof [和ferror ] 是錯誤的。
  4. 你有“平行” arrays ( countrycountryNames ),由國家計數/索引索引。 最好使用struct
  5. 不使用fileWithMedals 為什么它的長度是30 (固定/魔法常量)?
  6. 我猜這是要打開的文件名??? 但是,你打開"commonWealth.txt"
  7. fclose被調用了兩次 第二個電話會失敗。

這是重構的代碼。 它使用struct來組合數據。 它顯示了兩種讀取輸入的方法:

  1. 第一種方法是最簡單的代碼翻譯。 但是,它假定MAX_LENGTH_CNAMEMEDALCAT的硬連線值
  2. 第二種方法允許任意更改那些#define
#include <stdio.h>
#include <stdlib.h>

#define COUNTRIES 15                    // max number of countries
#define MEDALCAT 3                      // max number of medals / country
#define MAX_LENGTH_CNAME 100            // maximum country name length

struct country {
    char name[MAX_LENGTH_CNAME];        // country name
    int medals[MEDALCAT];               // medals won
};

int
readFromFile(const char *fileWithMedals,struct country countries[COUNTRIES])
{

    FILE *cWealth = fopen(fileWithMedals, "r");

    if (cWealth == NULL) {
        printf("Error! File was not created!");
        exit(1);
    }

    int count = 0;

    char buffer[1000];
    struct country *country;
    int *medals;

    while (fgets(buffer,sizeof(buffer),cWealth) != NULL) {
        // too many countries? should we error out here?
        if (count >= COUNTRIES)
            break;

        country = &countries[count];
        medals = country->medals;

        // syntax error -- should we error out here?
        if (sscanf(buffer,"%99s %d %d %d",
            country->name,&medals[0],&medals[1],&medals[2]) != 4)
            continue;

        ++count;
    }

    fclose(cWealth);

    for (int i = 0; i < count; i++) {
        country = &countries[i];
        medals = country->medals;
        printf("%s %d %d %d\n",
            country->name,medals[0],medals[1],medals[2]);
    }

    return count;
}

int
readFromFile2(const char *fileWithMedals,struct country countries[COUNTRIES])
{

    FILE *cWealth = fopen(fileWithMedals, "r");

    if (cWealth == NULL) {
        printf("Error! File was not created!");
        exit(1);
    }

    int count = 0;

    struct country *country;
    int *medals;

    // given that MAX_LENGTH_CNAME is 100, we want a format of "%99s" for
    // safety with fscanf
    char namefmt[10];
    sprintf(namefmt,"%%%ds",MAX_LENGTH_CNAME - 1);

    while (1) {
        // too many countries? should we error out here?
        if (count >= COUNTRIES)
            break;

        country = &countries[count];
        medals = country->medals;

        if (fscanf(cWealth,namefmt,country->name) != 1)
            exit(1);

        for (int i = 0;  i < MEDALCAT;  ++i) {
            if (fscanf(cWealth,"%d",&medals[i]) != 1)
                exit(2);
        }

        ++count;
    }

    fclose(cWealth);

    for (int i = 0; i < count; i++) {
        country = &countries[i];
        medals = country->medals;

        printf("%s",country->name);

        for (int j = 0;  j < MEDALCAT;  ++j)
            printf(" %d",medals[j]);

        printf("\n");
    }

    return count;
}

int
main(int argc,char **argv)
{

    --argc;
    ++argv;

    if (argc != 1) {
        fprintf(stderr,"need filename\n");
        exit(1);
    }

    struct country countries[COUNTRIES];

    printf("ORIGINAL:\n");
    readFromFile(argv[0],countries);

    printf("\nMODIFIED:\n");
    readFromFile2(argv[0],countries);

    return 0;
}

對於給定的輸入文件,這里是程序 output:

ORIGINAL:
Australia 67 57 54
England 57 66 53
Canada 26 32 34
India 22 16 23
NewZealand 20 12 17
Scotland 13 11 27
Nigeria 12 9 14
Wales 8 6 14
SAfrica 7 9 11
Uganda 3 0 2
Samoa 1 4 0
BVIslands 2 0 0
Ghana 0 2 3
Namibia 0 0 4
Dominica 0 2 0

MODIFIED:
Australia 67 57 54
England 57 66 53
Canada 26 32 34
India 22 16 23
NewZealand 20 12 17
Scotland 13 11 27
Nigeria 12 9 14
Wales 8 6 14
SAfrica 7 9 11
Uganda 3 0 2
Samoa 1 4 0
BVIslands 2 0 0
Ghana 0 2 3
Namibia 0 0 4
Dominica 0 2 0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM