Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.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_Multidimensional Array_Input - Fatal编程技术网

C 将多个输入行加载到二维数组中

C 将多个输入行加载到二维数组中,c,multidimensional-array,input,C,Multidimensional Array,Input,所以我不得不第一次使用2d阵列,我很困惑。 我想将多行(如示例(输入)中的行)加载到该数组中 123456 654321 123456 数组[0][0]上的值应为1,数组[1][0]-6。。 最重要的是线的长度是随机的,但每一行的长度都是一样的,我需要这个数字以备将来使用 最好的方法是什么?谢谢你的建议,请不要对我苛刻 谢谢这里有一个非常快速而肮脏的解决方案。当我处理这个问题时,您还没有指定数据类型或最大行长度,所以这适用于任何行长度。使用二维数组时,需要注意的重要部分是嵌套的for()循环。

所以我不得不第一次使用2d阵列,我很困惑。 我想将多行(如示例(输入)中的行)加载到该数组中

123456
654321
123456
数组[0][0]上的值应为1,数组[1][0]-6。。 最重要的是线的长度是随机的,但每一行的长度都是一样的,我需要这个数字以备将来使用

最好的方法是什么?谢谢你的建议,请不要对我苛刻


谢谢

这里有一个非常快速而肮脏的解决方案。当我处理这个问题时,您还没有指定数据类型或最大行长度,所以这适用于任何行长度。使用二维数组时,需要注意的重要部分是嵌套的for()循环。该程序的前半部分仅确定所需阵列的大小

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

int main(void){
    FILE* fin = fopen("fin.txt","r");
    char* line = NULL;
    size_t len = 0;
    int cols = 0, rows=1;
    int i=0, j=0;

    cols = getline(&line, &len, fin);
    cols--; // skip the newline character
    printf("Line is %d characters long\n", cols);
    while(getline(&line, &len, fin) != -1) rows++;
    printf("File is %d lines long\n", rows);
    rewind(fin);

    char array[rows][cols];
    char skip;
    for (i=0; i<rows; i++){
        for (j=0; j<cols; j++){
            fscanf(fin, "%c", &array[i][j]);
            printf("%c ", array[i][j]);
        }
        fscanf(fin, "%c", &skip); // skip the newline character
        printf("\n");
    }

    fclose(fin);
    return 0;
}
#包括
#包括
内部主(空){
文件*fin=fopen(“fin.txt”、“r”);
char*line=NULL;
尺寸长度=0;
int cols=0,rows=1;
int i=0,j=0;
cols=获取行(&line,&len,fin);
cols--;//跳过换行符
printf(“行长度为%d个字符,\n”,cols);
而(getline(&line,&len,fin)!=-1)行++;
printf(“文件长度为%d行,\n”,行);
倒带(鳍);
字符数组[行][cols];
字符跳过;
对于(i=0;i使用realloc,如下所示

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

int main(void){
    FILE *fp = stdin;//or fopen
    char line[100+2];
    int rows = 0, cols = 0;
    fgets(line, sizeof(line), fp);
    cols = strlen(line)-1;//-1 for newline

    char (*mat)[cols] = malloc(++rows * sizeof(char[cols]));
    memcpy(mat[0], line, cols);
    while(fgets(line, sizeof(line), fp)){
        if((mat = realloc(mat, ++rows * sizeof(char[cols]))) == NULL){//In the case of a large file, realloc a plurality of times together
            perror("realloc");
            exit(EXIT_FAILURE);
        }
        memcpy(mat[rows-1], line, cols);
    }
    //fclose(fp);
    //test print
    for(int r = 0; r < rows; ++r){
        for(int c = 0; c < cols; ++c){
            printf("%c ", mat[r][c]);
        }
        puts("");
    }
    free(mat);
}
#包括
#包括
#包括
内部主(空){
FILE*fp=stdin;//或fopen
字符行[100+2];
int行=0,cols=0;
fgets(行,sizeof(行),fp);
cols=strlen(行)-1;//-1表示换行符
char(*mat)[cols]=malloc(++rows*sizeof(char[cols]);
memcpy(mat[0],line,cols);
while(fgets(line,sizeof(line),fp)){
如果((mat=realloc(mat,++rows*sizeof(char[cols])))==NULL){//在大文件的情况下,realloc会同时执行多次
perror(“realloc”);
退出(退出失败);
}
memcpy(材料【第1行】、线、列);
}
//fclose(fp);
//试印
对于(int r=0;r
试着用代码中的赋值来计算。打印或使用调试器查看数组的变化。你决定了一行的最大长度吗?最大长度应该是100个符号。数据类型是
int
还是
char
?它必须是
char
谢谢。但是我真的不明白
(*mat)[cols]
。我从未见过这样的代码。你能给我解释一下吗?例如,我如何将数组传递给函数?其他代码部分对我来说很有意义。@Thomas
mat
是指向
char[cols]
的指针。