【问题标题】:Reading 2-D string Array From File In C从 C 中的文件中读取二维字符串数组
【发布时间】:2018-03-07 18:17:23
【问题描述】:

我是 C 的新手,正在尝试制作一个小程序。

基本上,它是一个程序,它从文本文件中获取元素名称、组、句点(不科学正确)编号。然后将元素名称插入二维字符串数组(代码中的elmName),将元素编号插入二维整数数组(elmNumber),然后将它们打印为元素周期表。

但是当我尝试运行它时,程序会获取它读取的最后一个元素名称并将其分配给 2d 字符串数组 (elmName) 中的每个元素。

代码如下:

char* elmName[18][5];

int elmNumber[18][5];
//AtomNumber[Group][Period] 

int iGroupCount = 18;
int iPeriodCount = 5;

for(int i=0;i<iPeriodCount;i++){
    for(int j=0;j<iGroupCount;j++){
        elmNumber[j][i] = 0;
    }
}

printf("\n");

for(int i=0;i<iPeriodCount;i++){
    for(int j=0;j<iGroupCount;j++){
        printf("%d ", elmNumber[j][i]);
    }
    printf("\n");
}

int g,p,num;
//g = Group
//p = Period
//num = Atom no.

int tempNum;
char tempName[2];

for(int i=0;i<iPeriodCount;i++){
    for(int j=0;j<iGroupCount;j++){
//if there is not an element at this group and period name it '*'
            elmName[j][i] = "*";
    }
}

FILE *fs = fopen("element.txt" , "r");
while(!feof(fs)){
    fscanf(fs ,"%d\t%d\t%d\t%s\n" , &g , &p , &tempNum , tempName);
    printf("%d\t%d\t%d\t%s\n" , g , p , tempNum , tempName);
    if( elmName[g][p] == "*"){
    elmNumber[g][p] = tempNum;
    elmName[g][p] = tempName;

}
     //g = group No
     //p = period No    
}

fclose(fs);

这是它读取的文本文件:

0   0   1   H
17  0   2   He
0   1   3   Li
1   1   4   Be
12  1   5   B
13  1   6   C
14  1   7   N
15  1   8   O
16  1   9   F
17  1   10  Ne
0   2   11  Na
1   2   12  Mg
12  2   13  Al
13  2   14  Si
14  2   15  P
15  2   16  S
16  2   17  Cl

如果您有解决方案,请提供帮助。 :)

【问题讨论】:

  • 使用字符串比较方法比较字符串。

标签: c arrays string multidimensional-array


【解决方案1】:

将此行char* elmName[18][5]; 更改为 char elmName[18][5][4]; 假设您的字符串不超过 3 个字符(3 个字符 + 1 个用于 NULL)。此行创建一个 3 维字符数组。

第 [i,j] 个元素将是一个字符串,在这种情况下您需要它: 还要更改此行:if( elmName[g][p] == "*")if( strcmp(elmName[g][p], "*")==0)。这是因为您无法使用相等运算符比较数组(字符串是字符数组)的值。

还将 elmName[g][p] = tempName; 更改为 strcpy(elmName[g][p],tempName)。 不要忘记包含 string.h 。您必须将每个字符复制到由strcpy 完成的字符数组中

以下是更正后的代码:

#include<stdio.h>
#include<string.h>
int main()
{
    char elmName[18][5][4];

int elmNumber[18][5];
//AtomNumber[Group][Period] 

int iGroupCount = 18;
int iPeriodCount = 5;

for(int i=0;i<iPeriodCount;i++){
    for(int j=0;j<iGroupCount;j++){
        elmNumber[j][i] = 0;
    }
}

printf("\n");

for(int i=0;i<iPeriodCount;i++){
    for(int j=0;j<iGroupCount;j++){
        printf("%d ", elmNumber[j][i]);
    }
    printf("\n");
}

int g,p,num;
//g = Group
//p = Period
//num = Atom no.

int tempNum;
char tempName[2];

for(int i=0;i<iPeriodCount;i++){
    for(int j=0;j<iGroupCount;j++){
//if there is not an element at this group and period name it '*'
            strcpy(elmName[j][i] , "*");//This line has been changed
    }
}

FILE *fs = fopen("element.txt" , "r");
while(!feof(fs)){
    fscanf(fs ,"%d\t%d\t%d\t%s\n" , &g , &p , &tempNum , tempName);
    printf("%d\t%d\t%d\t%s\n" , g , p , tempNum , tempName);
    if( strcmp(elmName[g][p], "*")==0)//This line has been changed
    {
    elmNumber[g][p] = tempNum;
    strcpy(elmName[g][p], tempName);//This line has been changed

}
     //g = group No
     //p = period No    
}


fclose(fs);
//For viewing results
for(int i=0;i<18;++i)
    for(int j=0;j<5;++j)
        printf("%d %d %s\n",i,j,elmName[i][j]);
    return 0;
}

这是我在使用您的示例文件时得到的输出

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0   0   1   H
17  0   2   He
0   1   3   Li
1   1   4   Be
12  1   5   B
13  1   6   C
14  1   7   N
15  1   8   O
16  1   9   F
17  1   10  Ne
0   2   11  Na
1   2   12  Mg
12  2   13  Al
13  2   14  Si
14  2   15  P
15  2   16  S
16  2   17  Cl
0 0 H
0 1 Li
0 2 Na
0 3 *
0 4 *
1 0 *
1 1 Be
1 2 Mg
1 3 *
1 4 *
2 0 *
2 1 *
2 2 *
2 3 *
2 4 *
3 0 *
3 1 *
3 2 *
3 3 *
3 4 *
4 0 *
4 1 *
4 2 *
4 3 *
4 4 *
5 0 *
5 1 *
5 2 *
5 3 *
5 4 *
6 0 *
6 1 *
6 2 *
6 3 *
6 4 *
7 0 *
7 1 *
7 2 *
7 3 *
7 4 *
8 0 *
8 1 *
8 2 *
8 3 *
8 4 *
9 0 *
9 1 *
9 2 *
9 3 *
9 4 *
10 0 *
10 1 *
10 2 *
10 3 *
10 4 *
11 0 *
11 1 *
11 2 *
11 3 *
11 4 *
12 0 *
12 1 B
12 2 Al
12 3 *
12 4 *
13 0 *
13 1 C
13 2 Si
13 3 *
13 4 *
14 0 *
14 1 N
14 2 P
14 3 *
14 4 *
15 0 *
15 1 O
15 2 S
15 3 *
15 4 *
16 0 *
16 1 F
16 2 Cl
16 3 *
16 4 *
17 0 He
17 1 Ne
17 2 *
17 3 *
17 4 *

您将最后添加的元素作为输出,因为您将字符串 tempName 的地址分配给二维字符数组指针 elmName。在我建议的解决方案中,我使用了静态 3 维字符数组,如果您想使用 2 维数组,则必须为其分配动态分配的字符数组(字符串),而不是在您的情况下为静态分配的变量 tempName

【讨论】:

    【解决方案2】:

    您应该使用指针数组将输入作为C 中的行。 2-D 数组不适合存储字符串。

     char *lineptr[MAXLINE];   //Array of pointers to store lines.
    
     int readline(char* lineptr[])
     {
      char line[1000];        //it takes line as input
      for(i=0;i<MAXLINE;i++){
           if(fscanf(stdin,"%s",line)){
              char *temp=malloc((strlen(line)+1)*sizeof(char));  //allocates memory for temp to store new line in every loop
              strcpy(temp,line);   //copies line to temp
              lineptr[i]=temp;     //lineptr[i] will point to new line i.e temp
           }
      }
      return i;   //returns the number of lines 
    }
    

    lineptr[] 中输入后,您可以根据需要操作输入行。

    【讨论】:

    • 我对指针了解不多。我会试试。感谢您的帮助! :)
    • @rtacr 我建议您阅读 Kernighan 和 Ratchie 的书 The C programming Language 以获得更好的概念。
    • 谢谢!我一定会去看看的。
    猜你喜欢
    • 2012-05-30
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-07
    • 1970-01-01
    相关资源
    最近更新 更多