0

How can I read the data from a file with structure like the one below into a multidimensional array of integers in C?

file:

3 4 30 29
23 43 4 43

I need put this inside of an "int** matrix" variable using dynamic allocation.

UPDATED:

I want an example code which I can go over and study the relation between the functionalities listed below:

  • multidimensional arrays and their relation with pointer-to-pointer;
  • dynamic allocation of memory and some explanation about it's use;
  • how to deal with data coming from an external source that we don't know the size of, how separate the rows/cols into our array inside of the C program.

SHARING CODE:

int** BuildMatrixFromFile(char* infile, int rows, int cols){

    FILE *fpdata;   //  deal with the external file
    int** arreturn; //  hold the dynamic array
    int i,j;        //  walk thru the array 

    printf("file name: %s\n\n", infile);

    fpdata = fopen(infile, "r");    //  open file for reading data

    arreturn = malloc(rows * sizeof(int *));
    if (arreturn == NULL)
    {
        puts("\nFailure trying to allocate room for row pointers.\n");
        exit(0);
    }

    for (i = 0; i < rows; i++)
    {

        arreturn[i] = malloc(cols * sizeof(int));
        if (arreturn[i] == NULL)
        {
            printf("\nFailure to allocate for row[%d]\n",i);
            exit(0);
        }

        for(j=0;j<cols;++j)
            fscanf(fpdata, "%d", &arreturn[i][j]);

    }

    fclose(fpdata); // closing file buffer

    return arreturn;    
}

Thank you.

5
  • 5
    As usual: What have you tried already? Commented Jan 25, 2011 at 20:47
  • What is your question about? The reading from files? Multi-dimensional arrays? Dynamic allocation? Pointers-to-pointers? Commented Jan 25, 2011 at 20:57
  • Firsty, without assumptions, please. Secondly, I updated the question to make it clear what I want. I wish this will help anyone that wants to perform the same action and did not found clear material about how to do it. Commented Jan 25, 2011 at 21:51
  • @Mariz: Each of your three bullet points would form a suitable question in its own right. The current question is too broad to be answered other than by writing the code, pretty much. Commented Jan 25, 2011 at 23:05
  • I can agree with you on this point. I built this function on my post now that is doing what I want, I'll put more comments later. Just wondering if is the best way to accomplish this task in C. Any comments about it are welcome. Thanks. Commented Jan 25, 2011 at 23:10

2 Answers 2

3

No-one is going to write your code for you. But here is a list of standard library functions that you might need to achieve this:

  • fopen()
  • fscanf()
  • fclose()
  • malloc()
  • free()
Sign up to request clarification or add additional context in comments.

4 Comments

Come on, this is a Q&A system, what kind of attitude is this? Did not liked the question, do not answer it. This will not help anyone to learn, Bye.
@Mariz: Questions that read like "please give me some code" are the worst kind. People here are happy to help with specific problems.
If you are starting to learn you should go to some other place? Got it, ok thanks.
@Mariz: No, that's not what I said! Your pissy attitude isn't doing you any favours, either.
-1

The description starting on page 20 of Numerical Recipes in C will show you one way to allocate the memory. Treat the 2d array as an array of pointers to the rows of your matrix.

Parsing the lines from the file is accomplished with fopen() and maybe fscanf() to pull out the numbers.

1 Comment

Thank you John the material seems promising

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.