Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 从文件中获取多个矩阵并将其存储在整数指针中_C_Arrays_File_Matrix - Fatal编程技术网

C 从文件中获取多个矩阵并将其存储在整数指针中

C 从文件中获取多个矩阵并将其存储在整数指针中,c,arrays,file,matrix,C,Arrays,File,Matrix,无效读取矩阵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的行数,第二行是矩阵A的列数,第二行是矩阵B的行数,第三行是矩阵B的列数 FILE * fp = fopen(file, "r"); char a, b, d; char c = getc(fp); int

无效读取矩阵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的行数,第二行是矩阵A的列数,第二行是矩阵B的行数,第三行是矩阵B的列数

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

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

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

这段代码在哪里尝试获取m、n和p?对于初学者来说,您只尝试获取一个字符。while循环中没有getc或fgetc…可能需要将while语句重写为while c=fgetcfp!=EOF。至于seg故障,我假设它发生在代码中的某个地方,还是在您未发布的部分?您是否尝试运行gdb并查看其失败的地方?如果您只读取了两个数组,为什么函数需要三个int**参数?您计划对阵列使用什么布局?它们将是二维数组还是一维数组,您可以对它们进行适当的索引?这很重要,因为如果它们是2D数组,很可能需要int***参数。传递未使用的参数应该被编译器捕获。是否将必要的标志传递给编译器以显示所有警告等?
$ ./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 ]
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);
    }
}