繁体   English   中英

从C中的文件读取二维字符串数组

[英]Reading 2-D string Array From File In C

我是C语言的新手,正在尝试编写一些程序。

基本上,这是一个程序,它从文本文件中获取元素名称,组,句号(科学上不正确)。 然后将元素名称插入2d字符串数组(代码中为elmName),将元素编号插入2d int数组(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行。 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[]接受输入后,您可以根据需要操纵输入行。

更改此行char* elmName[18][5]; 到char elmName [18] [5] [4]; 假设您的字符串不能超过3个字符(3个字符+ 1(表示NULL))。 这条线创建一个三维字符数组。

第[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的地址分配给2d字符数组指针elmName,因此您将获得最后添加的元素作为输出。 在我建议的解决方案中,如果要使用2维数组,则必须使用静态3维字符数组,而必须为其分配动态分配的字符数组(字符串),而不是在您的情况下使用tempName静态分配的变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM