简体   繁体   English

从文件读取并将其存储到C中的2D动态分配的数组中

[英]Reading from file and storing into 2D dynamically allocated array in C

I searched here, but nothing of what I found seems to work for me. 我在这里搜索,但是发现的所有内容似乎都不适合我。 I'm working on a project and I'm trying to implement a function which heading is the following: 我正在一个项目上,正在尝试实现以下功能:

read_from_file(*r, *col, fp);

\\param *r, where the number of rows of the new matrix is going to be stored; \\ param * r,将要存储新矩阵的行数的位置; \\param *col where the number of columns of the new matrix is going to be stored; \\ param * col将要存储新矩阵的列数的位置; \\param fp the file, already opened in reading mode in the main of the program, where to read from the matrix. \\ param fp该文件,已经在程序主程序中以读取模式打开,可以从矩阵中读取该文件。

\\retval must p, pointer to the new matrix, read from file. \\ retval必须p,指向新矩阵的指针,从文件中读取。

Now, each file is something like: 现在,每个文件都类似:

..........
.......@..
.....@@@@.
.....@@@..
..........
..........
..........
..........
..........
..........

Basically the program must have the option of uploading a sedimentation area, made of empty cells, the '.', and in case obstacles, the '@'. 基本上,该程序必须可以选择上载由空单元组成的沉淀区域,“。”,如果遇到障碍,则可以上载“ @”。 Some cells can also be full, '*'. 一些单元格也可以填满“ *”。 Those "special characters" are also defined like this: 这些“特殊字符”的定义也如下:

#define EMPTY '.'
#define FULL '*'
#define OBSTACLE '@'

The matrix representing the sedimentation area must be a 2D dynamically allocated array. 代表沉积区域的矩阵必须是2D动态分配的数组。 I wrote the piece of code about the evaluation of the number of rows and columns and it works just fine. 我写了一段有关评估行和列数的代码,它工作得很好。 I'm having issue trying to do the following: in the example file I wrote here, the matrix is a 10x10 one, so, one allocated enough space for the matrix: 我在尝试执行以下操作时遇到问题:在我在此处编写的示例文件中,矩阵是10x10的矩阵,因此,一个矩阵为矩阵分配了足够的空间:

char**p = malloc(row*sizeof(char*));
int i = 0;

for(i = 0; i < row; i++){
    p[i] = malloc(columns*sizeof(char));

What I'm trying to accomplish reading from file is: 我想要完成的从文件读取的操作是:

p[1][1] = '.';
p[1][2] = '.';
....
p[2][8] = '@';

And so on. 等等。 I've tried in many ways, but nothing seems to work. 我已经尝试了很多方法,但是似乎没有任何效果。 Once i try to write this new matrix on file, when I'm trying to read the output, there is an error message display which says that the file is corrupted. 一旦我尝试在文件上写入此新矩阵,当我尝试读取输出时,将显示错误消息,提示文件已损坏。 Can anybody help me? 有谁能够帮助我?

I tried like this: 我这样尝试过:

char*buffer = malloc(sizeof(char));
int k;

while(fgets(buffer, columns, fp) != NULL){
    for(i = 0; i < row; i++){
        k = 0;
        for(j = 0; j < columns; j++){
            if(j == col-1)
                p[i][j] = buffer[columns - 1]; /* excluding ‘\0’ here */
            else
                p[i][j] = buffer[k++];
            }
        }
}

I even tried like this: 我什至尝试这样:

    While((c = fgetc(fp))) != EOF){
        for(i = 0; i < row; i++){
              for(j=0; j< columns; j++){
                     p[i][j] = c;
                   }
             }
       }

Thanks in advance. 提前致谢。

How did you try to reading from file ? 您如何尝试从文件读取? I think you should check out how read a file line by line . 我认为您应该检查如何逐行读取文件

You can try that : 您可以尝试:

char**p = malloc(row*sizeof(char*));
char *line = NULL    
size_t len = 0;
int i = 0;

while ((getline(&line, &len, fp)) != -1) {
    p[i++] = strdup(line)
    printf("%s", line);
}

I think this should works, didn't test it on compilation, but I think you have the idea ! 我认为这应该可行,没有在编译时对其进行测试,但是我认为您有主意! as someone in the comment said, be careful with getline ! 正如评论中的某人所说,小心getline! It's not a standard C function and using it will not be portable to non-POSIX systems 它不是标准的C函数,使用它不能移植到非POSIX系统

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

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