0

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 *col where the number of columns of the new matrix is going to be stored; \param fp the file, already opened in reading mode in the main of the program, where to read from the matrix.

\retval must p, pointer to the new matrix, read from file.

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. 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:

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.

Chiara
  • 17
  • 1
  • 5
  • 3
    Error message from where? From your code? From the compiler? From the operating system? Can you please try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) and show us? And also please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). – Some programmer dude Jan 12 '18 at 08:28
  • 1
    Beware, this code `char**p = malloc(row*sizeof(char*)); int i = 0; for(i = 0; i < row; i++){ p[i] = malloc(columns*sizeof(char));` allocates [an array of pointers to array](https://stackoverflow.com/q/42094465/3545273) which is **not** the same as a 2D array. What is really expected, **I** don't know... – Serge Ballesta Jan 12 '18 at 08:40
  • 1
    To dynamically allocate a real 2D array you can use a pointer to VLA, if your compiler supports them: `char (*p)[row][columns] = malloc(sizeof *p)` – Ilja Everilä Jan 12 '18 at 09:33
  • Basically the sedimentation area must be a matrix at pointers. The professor said that we must allocated that matrix in this way. I think I had expressed myself badly sorry. – Chiara Jan 12 '18 at 09:44
  • *I've tried in many ways, but nothing seems to work*. But you failed to show any code attempt here. We can help you to fix errors or understand *specific* points but your only question is *Can anybody help me?*. The answer is obviously yes ;-), but we won't write the code from scratch for you... You should read (again?) [ask] – Serge Ballesta Jan 12 '18 at 10:21
  • Added some piece of my attempting code. – Chiara Jan 12 '18 at 11:24

1 Answers1

0

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 ! It's not a standard C function and using it will not be portable to non-POSIX systems

Sacha.R
  • 394
  • 2
  • 17
  • It should be noted that `getline` is not a standard C function and using it will not be portable to non-POSIX systems. – Some programmer dude Jan 12 '18 at 09:31
  • I tried, but it’s not what I need. I need to parse the file elem by elem and associate each elem to p[i][j]. Where i range goes from 0 to row and j range from 0 to columns. – Chiara Jan 12 '18 at 09:47
  • Be more specific about what you want to do. Actually, with the code I provided you, the array IS well associated if your file is correct. You can then add another function which takes the **p and return what you need ! I'm not sure I get what you want by "associate elem by elem" – Sacha.R Jan 12 '18 at 09:50
  • It should work with my solution. You can access p[I][j] for every characters ! – Sacha.R Jan 12 '18 at 11:54
  • It doesn't seem to work. It seems like I'm not able to read from the file! I don't understand why! – Chiara Jan 12 '18 at 20:53