简体   繁体   中英

How to read a string matrix from text file in C?

I have a text file whose name is matrix.txt . I try to read and assign this values to matrix[M][N] . I have a problem about reading. I know i have to use string but reading values' count is not stable. I want to assign values like hope.txt . How can this problem solve? What can i use instead of fscanf command?

matrix.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

This code is running but it is not after my own heart.

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;
}

I compiled my code in cygwin(2.4.0) with gcc (4.9.2)

output

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   

What I'm trying to do

Hope.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
.
.
.

I think your problem in linked to the way you store values. You are using a 2-dimensions array of chars, so it's normal you obtain only chars! You should better define a structure which fields should be of the type of each column (in order int, char, float, float, float). Then, you should store it as a 1-dimension array of struct. You can still use fscanf, but you should do one call per struct field, with a corresponding pattern for each's type.

Like @Nautigsam says here it is:

#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;
}

I tried it with stdin and works

With fgets() read a line from file and store it into an array of chars and then using sscanf function store contents into different variables.

something like this -

 #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);
     }

Change as follows

#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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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