Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C语言中如何将文件的第一行(字符串)和第一列(字符串)作为二维数组的索引_C_Arrays_String_File_Search - Fatal编程技术网

在C语言中如何将文件的第一行(字符串)和第一列(字符串)作为二维数组的索引

在C语言中如何将文件的第一行(字符串)和第一列(字符串)作为二维数组的索引,c,arrays,string,file,search,C,Arrays,String,File,Search,我想读取一个混合了字符串和数字的文件,并将其存储到2d数组中。条件是第一行和第一列应作为2d数组的索引。下面是示例文件 xx,yy aaa,10,11 bbb,12,13 ccc,14,15 ddd,16,17 eee,18,19 要清楚的是,我如何给字符串作为索引,我的数组应该给我这样的值 array[aaa][xx] = 10 array[bbb][yy] = 13... etc., 这是我的方法 FILE* fp1 = fopen("test.csv","r");

我想读取一个混合了字符串和数字的文件,并将其存储到2d数组中。条件是第一行和第一列应作为2d数组的索引。下面是示例文件

    xx,yy 
aaa,10,11
bbb,12,13
ccc,14,15
ddd,16,17
eee,18,19
要清楚的是,我如何给字符串作为索引,我的数组应该给我这样的值

array[aaa][xx] = 10
array[bbb][yy] = 13... etc.,
这是我的方法

    FILE* fp1 = fopen("test.csv","r");
    if(fp1 == NULL)
    {
       printf("Failed to open file\n");
       return 1;
    }
    char s[1] = ",";
    fscanf(fp1,"%[^\n]",array);   //  Read first row alone
    token = strtok(array,s);

    while( token != NULL )
    {
       strArray[i] = strdup(token);  // First row stored in strArray[i]
       strcpy(strArray[i], token);

       token = strtok(NULL, s);
       i++;
    }  
    i=0;
    while((fscanf(fp2,"%[^,],%[^,],%[^\n]\n",Col1,Col2,Col3)>0))   // Reading File Column wise
    {
        Column_1[i][j]= strdup(Col1); 
        Column_2[j]= atof(Col2);
        Column_3[j]=atof(Col3);
        j=j+1;
    }

    for(i=0;i<1;i++)
    {
      for(j=0;j<=5;j++)
      {
        myArray[i][j] = Column_2[j];
      }
    }
    for(i=1;i<2;i++)
    {
      for(j=0;j<=5;j++)
      {
        myArray[i][j] = Column_3[j];     // Column 2 & 3 values stored in myArray[i][j]
      }
    }      
FILE*fp1=fopen(“test.csv”、“r”);
如果(fp1==NULL)
{
printf(“打开文件失败\n”);
返回1;
}
字符s[1]=“,”;
fscanf(fp1,“%[^\n]”,数组);//单独阅读第一行
令牌=strtok(数组,s);
while(令牌!=NULL)
{
strArray[i]=strdup(令牌);//存储在strArray[i]中的第一行
strcpy(strArray[i],令牌);
令牌=strtok(空,s);
i++;
}  
i=0;
而((fscanf(fp2,“%[^,]”,“%[^,]”,%[^\n]\n”,Col1,Col2,Col3)>0))//按列读取文件
{
_1[i][j]列=strdup(Col1);
列_2[j]=atof(Col2);
列_3[j]=atof(Col3);
j=j+1;
}

对于(i=0;i以我有限的知识,我真的不认为您可以声明类型为
char
的2D数组并获取
int
值。相反,我建议使用结构。 这里有一个指向结构的内联链接::() 另一个简单的解决方案是静态声明第一行和第一列,将输入作为字符,并通过
atoi()
函数将其转换为整数;
     FILE* fp1 = fopen("test.csv","r");
     if(fp1 == NULL)
     {
       printf("Failed to open file\n");
       return 1;
     }
     char s[1] = ",";
     fscanf(fp1,"%[^\n]",array);   //  Read first row alone
     token = strtok(array,s);

     while( token != NULL )
     {
       strArray[i] = strdup(token);  // First row stored in strArray[i]
       strcpy(strArray[i], token);

       token = strtok(NULL, s);
       i++;
     }  
     i=0;
     while((fscanf(fp2,"%[^,],%[^,],%[^\n]\n",Col1,Col2,Col3)>0))   // Reading File Column wise
     {
       Column_1[i][j]= strdup(Col1); 
       Column_2[j]= atof(Col2);
       Column_3[j]=atof(Col3);
       j=j+1;
     }

     for(i=0;i<1;i++)
     {
       for(j=0;j<=5;j++)
       {
         myArray[i][j] = Column_2[j];
       }
     }
     for(i=1;i<2;i++)
     {
       for(j=0;j<=5;j++)
       {
         myArray[i][j] = Column_3[j];     // Column 2 & 3 values stored in myArray[i][j]
       }
     } 
     printf("\n\nEnter Row_name to search\t");
     scanf("%s", Row_name);
     printf("\nEnter Column_name to search\t");
     scanf("%s", Column_name);     
     for(i=0;i<1;i++)
     {
       for(j=0;j<=5;j++)
       {
         if(strcmp(Row_name,Column_1[i][j]) == 0)
         {
           i_index = j;
         }
       }
     }
     for(i=0;i<3;i++)
     {
      if(strcmp(Column_name,strArray[i]) == 0)
      {
       j_index = i;

      }
     }
     printf("\nValue[%s][%s] is '%f'",Row_name,Column_name,myArray[j_index-1][i_index] );
     fclose(fp1);
     return 0;  
     }
如果(fp1==NULL) { printf(“打开文件失败\n”); 返回1; } 字符s[1]=“,”; fscanf(fp1,“%[^\n]”,数组);//仅读取第一行 令牌=strtok(数组,s); while(令牌!=NULL) { strArray[i]=strdup(令牌);//存储在strArray[i]中的第一行 strcpy(strArray[i],令牌); 令牌=strtok(空,s); i++; } i=0; 而((fscanf(fp2,“%[^,]”,“%[^,]”,%[^\n]\n”,Col1,Col2,Col3)>0))//按列读取文件 { _1[i][j]列=strdup(Col1); 列_2[j]=atof(Col2); 列_3[j]=atof(Col3); j=j+1; }
对于(i=0;i而言,我今天使用的方法是:

string[][] values = new string[2][];//string = open value|new string = fill value
  int i = 0;      
        while (!sr.EndOfStream)
        {               
            strline = sr.ReadLine();
            values[i] = strline.Split(',');//'i' is row 1
            i++;// this is row too
            } };       
        }
        sr.Close(); // Release the file.
        Console.WriteLine(values[0][0]);//test your code with this value for row1 and column1

制作索引和名称的对应表。感谢您的宝贵建议。仅供参考,我声明了类型为float的2D数组,并通过atof()获取值,正如我在上述代码中提到的。列_2[j]=atof(Col2);列_3[j]=atof(Col3);谢谢,因为,我没有收到任何我发布的答案,我觉得有点复杂,但它对我来说很好。。。