【问题标题】:How to read a string matrix from text file in C?如何从C中的文本文件中读取字符串矩阵?
【发布时间】:2015-10-06 23:02:57
【问题描述】:

我有一个名为matrix.txt 的文本文件。我尝试读取这些值并将其分配给matrix[M][N]。我有一个关于阅读的问题。我知道我必须使用字符串,但读取值的计数并不稳定。我想分配像hope.txt 这样的值。这个问题怎么解决?我可以用什么来代替fscanf 命令?

矩阵.txt

1   X   3.37582 -1.01229 -0.00022
2   X   2.26583 -0.14632  0.00005
3   X   0.76713 -1.96181  0.00014
4   X   1.8336  -2.88677 -0.00015
5   X   3.1528  -2.40381 -0.00035
6   Y   4.38666 -0.61970 -0.00036
7   Y  -0.27476 -2.26966  0.00033
8   Y   1.62133 -3.95092 -0.00017
9   Y   3.99265 -3.09253 -0.00057
10  X   2.39233  1.35970  0.00026
11  Z   1.36086  2.07407  0.00012
12  X   3.77095  1.98739  0.00069
13  Y   4.34126  1.68147  0.88735
14  Y   4.34154  1.68204 -0.88601
15  Y   3.66765  3.07442  0.00099
16  T   0.99103 -0.62838  0.00021
17  L  -0.85577  0.79435 -0.00032
18  T  -3.41216 -0.53167  0.00027
19  Z  -3.08467  0.78325 -0.00030
20  Z  -4.63453 -0.87635  0.00045
21  Z  -2.42389 -1.39794  0.00066

这段代码正在运行,但它并不符合我的心意。

strread.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define M 20
#define N 5
int main(int argc, char *argv[])
{
    int i=0; int j=0;
    char matrix[M][N];
    FILE *fp;

    fp=fopen("matrix.txt","r");
    if(fp==NULL)
        exit(1);

    for(i=1;i<=M;i++){
        for(j=1;j<=N;j++){
            if(matrix[i][j]!=' '){
                fscanf(fp,"%c",&matrix[i][j]);
                printf("M[%d,%d]=%c\t",i,j,matrix[i][j]);
            }
        }
        printf("\n");
    }

    fclose(fp);
    return 0;
}

我在 cygwin(2.4.0) 中使用 gcc (4.9.2) 编译了我的代码

输出

M[1,1]=1    M[1,2]=     M[1,3]=X    M[1,4]=     M[1,5]=3    
M[2,1]=.    M[2,2]=3    M[2,3]=7    M[2,4]=5    M[2,5]=8    
M[3,1]=2    M[3,2]=     M[3,3]=-    M[3,4]=1    M[3,5]=.    
M[4,1]=0    M[4,2]=1    M[4,3]=2    M[4,4]=2    M[4,5]=9    
M[5,1]=     M[5,2]=-    M[5,3]=0    M[5,4]=.    M[5,5]=0    
M[6,1]=0    M[6,2]=0    M[6,3]=2    M[6,4]=2    M[6,5]=

M[7,1]=
    M[7,2]=2    M[7,3]=     M[7,4]=X    M[7,5]=     
M[8,1]=2    M[8,2]=.    M[8,3]=2    M[8,4]=6    M[8,5]=5    
M[9,1]=8    M[9,2]=3    M[9,3]=     M[9,4]=-    M[9,5]=0    
M[10,1]=.   M[10,2]=1   M[10,3]=4   M[10,4]=6   M[10,5]=3   
M[11,1]=2   M[11,2]=    M[11,3]=    M[11,4]=0   M[11,5]=.   
M[12,1]=0   M[12,2]=0   M[12,3]=0   M[12,4]=0   M[12,5]=5   
M[13,1]=
    M[13,2]=
    M[13,3]=3   M[13,4]=        M[13,5]=X   
M[14,1]=        M[14,2]=0   M[14,3]=.   M[14,4]=7   M[14,5]=6   
M[15,1]=7   M[15,2]=1   M[15,3]=3   M[15,4]=        M[15,5]=-   
M[16,1]=1   M[16,2]=.   M[16,3]=9   M[16,4]=6   M[16,5]=1   
M[17,1]=8   M[17,2]=1   M[17,3]=    M[17,4]=    M[17,5]=0   
M[18,1]=.   M[18,2]=0   M[18,3]=0   M[18,4]=0   M[18,5]=1   
M[19,1]=4   M[19,2]=
    M[19,3]=
    M[19,4]=4   M[19,5]=        
M[20,1]=X   M[20,2]=        M[20,3]=1   M[20,4]=.   M[20,5]=8   

我想做什么

希望.txt

M[1,1]=1   M[1,2]=X   M[1,3]=3.37582 M[1,4]=-1.01229 M[1,5]=-0.00022
M[2,1]=2   M[2,2]=X   M[2,3]=2.26583 M[2,4]=-0.14632 M[2,5]=0.00005
.
.
.

【问题讨论】:

    标签: c string file matrix scanf


    【解决方案1】:

    我认为您的问题与您存储值的方式有关。您使用的是二维字符数组,因此您只获得字符是正常的!您应该更好地定义一个结构,哪些字段应该是每列的类型(顺序为 int、char、float、float、float)。 然后,您应该将其存储为结构的一维数组。 您仍然可以使用 fscanf,但您应该对每个结构字段进行一次调用,并为每个类型使用相应的模式。

    【讨论】:

      【解决方案2】:

      就像@Nautigsam 所说的:

      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      
      #define M 20
      typedef struct {
          int a;
          char b;
          float c, d, e;
      } line;
      int main(int argc, char *argv[])
      {
          int i = 0; int j = 0;
          line matrix[M];
          FILE *fp;
      
          fp = fopen("matrix.txt", "r");
          if (fp == NULL)
              exit(1);
      
          for (i = 1; i <= M; i++){
              fscanf(fp, "%i\t%c%f%f%f", &matrix[i].a, &matrix[i].b, &matrix[i].c, &matrix[i].d, &matrix[i].e);
              printf("%i\t%c\t%f\t%f\t%f\n", matrix[i].a, matrix[i].b, matrix[i].c, matrix[i].d, matrix[i].e);
          }
      
          fclose(fp);
          return 0;
      }
      

      我用stdin 试了一下,效果很好

      【讨论】:

        【解决方案3】:

        如下改变

        #define M 21  //you have 21 line data
        
        char matrix[M][N][9];
        
        for(i=1;i<=M;i++){
            for(j=1;j<=N;j++){
                fscanf(fp,"%8s",matrix[i-1][j-1]);
                printf("M[%d,%d]=%s\t",i,j,matrix[i-1][j-1]);
            }
            printf("\n");
        }
        

        【讨论】:

        • @bdllhtlgn 最多读取 8 个字符。 (9 : 8 + '\0')
        【解决方案4】:

        使用fgets() 从文件中读取一行并将其存储到一个字符数组中,然后使用sscanf 函数将内容存储到不同的变量中。

        类似的东西-

         #define MAX_LENGTH 256
        
         char matrix[MAX_LENGTH];
        
         while(fgets(matrix,MAX_LENGTH,fp))
             {
                   sscanf(matrix,"%d %c %f ",variable 1,varable 2,varable 3);
             }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-06-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-20
          • 1970-01-01
          相关资源
          最近更新 更多