【问题标题】:Read file into multidimensional character array in C将文件读入C中的多维字符数组
【发布时间】:2017-05-16 19:27:45
【问题描述】:

我正在尝试从文件中读取多维字符串矩阵并将其存储到字符数组数组中,如下所示:

char *A[M][N];

如果我留在主函数中,这会很好。 但是当我试图通过引用调用函数时它不起作用。

函数调用:

readMatrix(file,A);

函数头:

int readMatrix(FILE *file,char *matrix[][]);

我也试过这个:

int readMatrix(FILE *file,char ***matrix);

这也不起作用。 我想在函数中操作数组,因此我需要进行引用调用。

【问题讨论】:

  • 您需要添加填充数组的代码
  • 尝试int readMatrix(FILE *file, size_t m, size_t n, char *matrix[m][n]);并致电readMatrix(file, M, N, A);
  • 什么不起作用?请提供更多详细信息并粘贴 readMatrix 函数的代码
  • char* 的二维矩阵与char*** 不同。你的编译器不应该接受这些。
  • int readMatrix(FILE *file, size_t m, size_t n, char *matrix[m][n]);读取矩阵(文件,M,N,A);这很好用,非常感谢;-)

标签: c file pointers multidimensional-array


【解决方案1】:

在函数 readMatrix 中还需要传递 M 和 N 的值,否则无法知道每个 A[i] 的准确位置,因此无法知道每行的加载位置。

【讨论】:

    【解决方案2】:

    您必须将 N 作为矩阵类型的一部分传递给您的 ReadMatrix 函数,并且由于它在编译时未知,因此您也必须将这些作为参数传递:

    int readMatrix(FILE *file, size_t M, size_t N, char *matrix[][N]);
    

    您确实也可以将数组参数指定为char *matrix[M][N],但函数参数的第一维大小M 会被忽略,因为它只接收指向数组的指针。

    这是一个例子:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int readMatrix(FILE *file, size_t rows, size_t cols, char *matrix[][cols]) {
        int rc = 0;
        for (size_t i = 0; i < rows; i++) {
            for (size_t j = 0; j < cols; j++) {
                char buf[80];
                if (rc == 0 && fscanf(file, "%79s", buf) == 1) {
                    matrix[i][j] = strdup(buf);
                } else {
                    matrix[i][j] = NULL;
                    rc = -1;
                }
            }
        }
        return rc;
    }
    
    int main(void) {
        size_t rows, cols;
        if (scanf("%zu %zu", &rows, &cols) != 2)
            return 1;
        char *mat[rows][cols];
        if (readMatrix(stdin, rows, cols, mat) == 0) {
            for (size_t i = 0; i < rows; i++) {
                for (size_t j = 0; j < cols; j++) {
                    printf(" %8s", mat[i][j]);
                }
                printf("\n");
            }
        }
        for (size_t i = 0; i < rows; i++) {
            for (size_t j = 0; j < cols; j++) {
                free(mat[i][j]);
            }
        }
        return 0;
    }
    

    【讨论】:

    • size_t N会不会有问题?不会被100代替吗?
    • 你可以省略最左边的暗淡。不是很暗。
    • 大小始终是文件的第一行。读取第一行后调用 readMatrix 方法
    • @informaticienzero:正确!我用一个工作示例更新了代码。只需将字典重定向到程序的标准输入。
    • @BLUEPIXY:我错过了 cmets 中提供的重要信息。固定
    【解决方案3】:

    您可以使用这种方法来做到这一点:

    #include <stdio.h>
    #define MAX_WORD_SIZE 10
    void readMatrix(FILE*,char****);
    int main(void){
        FILE* cin = fopen("input.txt","r");
        char*** inputMatrix;
        readMatrix(cin,&inputMatrix);
        for(int i=0;i<3;i++){
                for(int j=0;j<3;j++){
                    printf("%s ",*(*(inputMatrix+i)+j));
                }
                printf("\n");
        }
        fclose(cin);
        return 0;
    }
    void readMatrix(FILE* cin,char**** matrix){
        int rowNum,columnNum;
        char buff[10];
        int intBuff;
        fscanf(cin,"%d",&intBuff);
        rowNum = intBuff;
        fscanf(cin,"%d",&intBuff);
        columnNum = intBuff;
        *matrix = malloc(rowNum*sizeof(char**));
        for(int i=0;i<rowNum;i++){
            *(*matrix+i) = malloc(columnNum*sizeof(char*));
        }
    
        for(int i=0;i<rowNum;i++){
            for(int j=0;j<columnNum;j++){
                *(*(*matrix+i)+j) = malloc(MAX_WORD_SIZE*sizeof(char));
                fscanf(cin,"%s",*(*(*matrix+i)+j));
            }
        }
    }
    

    使用此输入文件(同一文件夹中的 input.txt 文件),此代码在 Linux GCC 上运行良好

    3 3
    ab cd ef
    gh ij kl
    mn op qr
    

    在input.txt文件的第一行,第一个整数表示行号,第二个整数表示列号。

    【讨论】:

      猜你喜欢
      • 2020-07-08
      • 1970-01-01
      • 1970-01-01
      • 2012-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多