【问题标题】:Grab multiple matrices from a file and store it in a integer pointer从文件中获取多个矩阵并将其存储在整数指针中
【发布时间】:2014-11-22 02:44:44
【问题描述】:

void read_matrices(int **A, int **B, int **C, int *m, int *n, int *p, char *file)

我想从一个如下所示的外部文件中读取两个矩阵:

 3 
 2 
 4 
 1 2 
 3 4 
 5 6 
 7 8 9 10 
 11 12 13 14

其中第一行的数字是矩阵 A 的行数,数字 2 是矩阵 A 的列数和矩阵 B 的行数,第三个数字是矩阵 B 的列数。我试过这个来获取 m、n 和 p,但得到一个 seg 错误

FILE * fp = fopen(file, "r");
char a, b, d;
char c = getc(fp);
int i = 0;
while (c != EOF) {
    if (i == 0) {
        a = c;
        i++;
    }
    else if (i == 1) {
        b = c;
        i++;
    }
    else if (i == 2) {
        d = c;
        i++;
    }
    else
        break;
}
fclose(fp);

【问题讨论】:

  • 这段代码在哪里尝试获取mnp
  • 对于初学者来说,您只尝试获取一个字符。 while 循环内没有 getc 或 fgetc ......可能想将 while 语句重写为 while ( (c = fgetc(fp)) != EOF ) 之类的东西。至于段错误,我假设它发生在这段代码的某个地方,或者是你没有发布的部分?您是否尝试过运行 gdb 并查看失败的地方?
  • 如果你只读取两个数组,为什么函数需要三个int ** 参数?您打算为阵列使用什么布局?它们将是您适当索引的二维数组还是一维数组? (这很重要,因为如果它们是二维数组,你很可能需要int *** 参数。)
  • 传递未使用的参数应该已被编译器捕获。您是否将必要的标志传递给编译器以显示所有警告等?

标签: c arrays file matrix


【解决方案1】:

很明显,您需要一些帮助。这是一个示例,它只是许多方法中的一种。 注意: 读取B 中可变数量的元素的示例并不复杂,而是简单地编码为读取数据文件中存在的4 元素。此外,fscanf 用于从文件中读取,因为您在每一行中都有一个可变编号和不同的数据要读取。与逐字符读取文件相比,这简化了代码,因为读取的信息类型是int。通读示例(cmets inline),如果您有任何问题,请告诉我:

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

int main (int argc, char **argv) {

    int *A, *B, *C, m, n, p;  /* note *C is unused */
    char *file;

    /* test for required filename */
    if (argc < 2) {
        fprintf (stderr, "\n Error: insufficient input. usage: %s filename\n\n", argv[0]);
        return 1;
    }

    /* assign filename, open file, validate opening */
    file = argv[1];  /* you don't need 'file', you can simply use argv[1] */

    FILE *fp = fopen (file, "r");
    if (!fp) {
        fprintf (stderr, "\n Error: file open failed for file '%s'\n\n", argv[0]);
        return 1;
    }

    /* read & output m, n, p */
    fscanf (fp, "%d\n%d\n%d\n", &m, &n, &p);
    printf ("\n m: %d\n n: %d\n p: %d\n\n", m, n, p);

    /* allocate memory for A and set values to null */
    A = calloc (m * 2, sizeof(int));

    /* read A */
    int i = 0;
    for (i = 0; i < m; i++)
        fscanf (fp, "%d %d\n", &A [i*2], &A[i*2 + 1]);

    /* output A */
    printf ("Array A\n");
    for (i = 0; i < m; i++)
        printf (" [ %d  %d ]\n", A [i*2], A[i*2 + 1]);

    /* allocate memory for B and set values null */
    B = calloc (n * p, sizeof(int));

    /* read B */
    for (i = 0; i < n; i++)
        fscanf (fp, "%d %d %d %d\n", &B [i*p], &B[i*p + 1], &B[i*p + 2], &B[i*p + 3]);

    /* output B */
    printf ("\nArray B\n");
    for (i = 0; i < n; i++)
        printf (" [ %2d %2d %2d %2d ]\n", B [i*p], B[i*p + 1], B[i*p + 2], B[i*p + 3]);

    printf ("\n");

    /* close FP & free allocated memory */
    fclose (fp);
    if (A) free (A);
    if (B) free (B);

    return 0;
}

输出:

$ ./bin/rmf dat/array.dat

m: 3
n: 2
p: 4

Array A
 [ 1  2 ]
 [ 3  4 ]
 [ 5  6 ]

Array B
 [  7  8  9 10 ]
 [ 11 12 13 14 ]

【讨论】:

    【解决方案2】:
    for the bit of code you provided, 
    which fails to set the m, n, p variables 
    and fails to read/set the contents of the matrices A, B
    and fails to read/set the contents of matrix C.
    
    It is also very dangerous to pass parameters pointing to predefined matrix sizes
    as there is no guarantee that the size values read from the file
    will fit into the pre-allocated areas.
    
    Much better to only pass a pointer to each matrix pointer,
    then malloc the matrix sizing after the actual values are read
    and set the passed matrix pointer A,B,C to point to the malloc'd areas
    and read the matrix data into the malloc'd areas.
    
    
    void read_matrices(int **A, int **B, int **C, int *m, int *n, int *p, char *file)
    {
        FILE * fp = fopen(file, "r");
        if( NULL == fp ) // then handle fopen() error
        else
        {
            int aRows = getc(fp);
            int aCols = getc(fp);
            int bRows = aCols;
            int bCols = getc(fp);
    
            // need to add code to read the A, B matrix contents
            // need to add code to read the C matrix contents
            // need to add code to set m,n,p 
    
            fclose(fp);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-30
      • 1970-01-01
      • 2015-03-15
      相关资源
      最近更新 更多