简体   繁体   English

在C中使用fscanf与不同宽度的文件

[英]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. 显然,由于在读取文件时fscanf中缺少参数,此刻此刻不起作用。 How can I use fscanf to read the values into the matrix? 如何使用fscanf将值读入矩阵?

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 使用strtok()将行分割为带分隔符的标记
use strtof() to convert string to float 使用strtof()将字符串转换为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);
}

在此处输入图片说明

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

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