![](/img/trans.png)
[英]How can I create a 2D array to store a collection of words scanned from a .txt file in C?
[英]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列。
幾個問題...
char single = fgetc(cWealth);
應該在if (cWealth == NULL)
塊之后完成。-Wall
feof
[和ferror
] 是錯誤的。country
和countryNames
),由國家計數/索引索引。 最好使用struct
。fileWithMedals
。 為什么它的長度是30
(固定/魔法常量)?"commonWealth.txt"
?fclose
被調用了兩次。 第二個電話會失敗。 這是重構的代碼。 它使用struct
來組合數據。 它顯示了兩種讀取輸入的方法:
MAX_LENGTH_CNAME
和MEDALCAT
的硬連線值#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.