简体   繁体   中英

using fscanf in C with varying width files

I need to write a program that reads in various files and stores the information into arrays. I will be using the doubles to perform matrix multiplies. Regarding the format of the files; the first line contains the size of the matrix. The next several lines are the rows of the matrix, where each element is separated by a space.

Format:

<number of rows> <number of columns>
<double> <double> ... <double>
<double> <double> ... <double>
.
.
.
<double> <double> ... <double>

Here are couple example files:

3 4
1.20 2.10 4.30 2.10
3.30 3.10 5.20 2.80
1.10 0.60 4.70 4.90

or

5 5
1.20 2.10 4.30 2.10 6.70
3.30 3.10 5.20 2.80 3.20
1.10 0.60 4.70 4.90 9.10
3.30 3.10 5.20 2.80 3.20
1.10 0.60 4.70 4.90 7.10

At the moment my code is as follows:

float** readFile(char* fp)
{
    float** matrix = (float**)malloc(M*N*sizeof(float));

    fp = fopen(fp, "r");

    if (fp == NULL)
    {
        fprintf(stderr, "Can't open the file\n");
        exit(1)
    }

    int i = 0;
    int m, n;
    fscanf(fp, "%d %d", m, n);
    while (fscanf(fp, "");
    {
        i++;
    }

    fclose(fp);

    return matrix;
}

and I am calling the function like this:

float** A = readFile(argv[1]);

Obviously this won't work at the moment because of the missing arguments in fscanf while reading the file. How can I use fscanf to read the values into the matrix?

Modify this function

float** readFile(char* file) 
{
    FILE *fp;
    float** matrix = (float**)malloc(M*N*sizeof(float));

    fp = fopen(file, "r");

    if (fp == NULL)
    {
        fprintf(stderr, "Can't open the file\n");
        exit(1)
    }

    int i = 0;
    int m, n;
    fscanf(fp, "%d %d", m, n);
    while (fgets(line,size(line),fp)!=NULL) //read file line by line
    {

use strtok() to split line into tokens with delimiter space
use strtof() to convert string to float

    }

    fclose(fp);

    return matrix;
}

To simply things, let us use one dimensional array. Here is the working code for you.

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

float* readFile(char* fp, int *m, int *n)/* return the dimension defined in file*/
{

    FILE *file = fopen(fp, "r");

    if (file == NULL)
    {
        fprintf(stderr, "Can't open the file\n");
                exit(1);
    }

    int i = 0, j = 0;
    fscanf(file, "%d %d[^\n]\n", m, n);
    float* matrix = (float*)malloc((*m)*(*n)*sizeof(float));

    float f;
    for (i = 0; i < *m ; ++i)
    for (j = 0; j < *n ; ++j)
    {
       fscanf(file, "%f", (matrix + i * (*n) + j));
    }


    fclose(file);

    return matrix;
}

int main()
{
    int m, n, i, j;
    float *a = readFile("a.dat", &m, &n); /* i named your data file a.dat*/

    for (i = 0; i < m ; ++i)
        {
                for (j = 0; j < n ; ++j)
                   printf("%f ", *(a + i * n + j));

                printf("\n");
        }

        free(a);
}


    /* this is the output */
1.200000 2.100000 4.300000 2.100000 6.700000
3.300000 3.100000 5.200000 2.800000 3.200000
1.100000 0.600000 4.700000 4.900000 9.100000
3.300000 3.100000 5.200000 2.800000 3.200000
1.100000 0.600000 4.700000 4.900000 7.100000

Here is one more version which reads the data into dynamically allocated two-dimensional array:

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

float** arralloc(int r, int c) {
    int i;
    float** arr = (float**) malloc(r*sizeof(float*));
    for (i = 0; i < r; i++)
        arr[i] = (float*) malloc(c*sizeof(float));
    return arr;
}

int main(void)
{
    int c,r,i,j;
    FILE *file = fopen("file", "r");
    fscanf(file, "%d %d\n", &r, &c);
    float** arr = arralloc(r,c);
    for (i = 0; i < r; i++){
        for (j = 0; j < c; j++){
            fscanf(file, "%f", &arr[i][j]);
            printf("%.2f ", arr[i][j]);
        }
        printf("\n");
    }
    fclose(file);
    free(arr);
}

在此处输入图片说明

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