Does anyone know how to do this:

I need to read data from a .txt file into a multidimensional array that is dynamically allocated from memory.

How would one do that?

Thanks to whoever can help me!

so what's your question? and what have you done so far?

go ahead and show us what you've got. here's a handy template:

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

int main(void)
{

   // paste code here

   return 0;
}

oh, wait? you want hints on how to start? okay, here are the basic functions, in roughly the order you'll want to use them. give or take...

malloc
fopen
while
fgets
if / else
realloc
fclose
free

there's probably a few more, but don't say i didn't give you anything.


.

Here's what I have, but I know I'm not doing it right.

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

FILE  *getOpen();         // Function Prototype

int main()
{  
   
   FILE        *inFile;  // Declares inFile pointer as a FILE type.
   STACK       *stack;   // Declares stack pointer as a STACK type.
   int         *matrix; // Declares matrix pointer as a int type.
   int          nrows;   // number of rows.
   int          ncolumns;// number of columns.
   int          i;
   
   // Open the text file containing the matrix.
   inFile = getOpen();
   
   // Dynamically allocate memory for the read in matrix.
   matrix = (int *) malloc(nrows * sizeof(int));
   if(matrix == (int *) NULL)
   {
        printf("Out of memory.\n");
        exit(1);
   }
   for(i = 0; i < nrows; i++)
   {
        matrix[i] = (int *) malloc(ncolumns * sizeof(int);
        if(array[i] == NULL)
        {
             printf("Out of memory.\n");
             exit(1);
        }
   }
   
   
   system("pause");
   return 0;
   
}
   
/* =============================================
                   getOpen
   =============================================
   This function opens the file for reading, and 
   returns the file name back to the program for 
   use.
*/
FILE *getOpen()
{
     FILE *fname;
     char name[21];
     
     printf("\nEnter a file name: ");
     gets(name);
     fname = fopen(name, "r");
     if (fname == NULL)
     {
          printf("Failed to open the file %s.\n", name);
          exit(1);
     }
     printf("File was successfully opened!\n");
     
     return(fname);
}

okay, that's a start. what's your question?

I don't think I'm allocating the memory right for a 2D array. I'm also stuck on how to read the matrix from the file into the dynamically allocated array.

How do you do that?

Am I close? Did I allocate the memory right? It seemed to work......

Purpose:             Given a square matrix, this program determines the number of white blocks
                        and total number of squares in each of the white blocks.  The square 
                        is read in from a text file, and a printed report is printed showing
                        the number of white blocks and the number of squares in each.
*/

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

FILE  *getOpen();         // Function Prototype

int main()
{  
   
   FILE        *inFile;  // Declares inFile pointer as a FILE type.
   STACK       *stack;   // Declares stack pointer as a STACK type.
   int         **matrix; // Declares matrix pointer as a int type.
   int          nrows;   // number of rows.
   int          ncolumns;// number of columns.
   int          i;
   
   // Open the text file containing the matrix.
   inFile = getOpen();
   
   // Dynamically allocate memory for the read in matrix.
   matrix = malloc(nrows * sizeof(int *));
   if(matrix == NULL)
   {
        printf("Out of memory.\n");
        exit(1);
   }
   printf("Memory allocated successfully!\n");
   for(i = 0; i < nrows; i++)
   {
        matrix[i] = malloc(ncolumns * sizeof(int *));
        if(matrix[i] == NULL)
        {
             printf("Out of memory.\n");
             exit(1);
        }
   }
   
   
   system("pause");
   return 0;
   
}
   
/* =============================================
                   getOpen
   =============================================
   This function opens the file for reading, and 
   returns the file name back to the program for 
   use.
*/
FILE *getOpen()
{
     FILE *fname;
     char name[21];
     
     printf("\nEnter a file name: ");
     gets(name);
     fname = fopen(name, "r");
     if (fname == NULL)
     {
          printf("Failed to open the file %s.\n", name);
          exit(1);
     }
     printf("File was successfully opened!\n");
     
     return(fname);
}

re: allocating your array

matrix = (int *) malloc(nrows * sizeof(int));
if(matrix == (int *) NULL)

what is your "nrows"?? its uninitialized. you need to specify what the size of the array you want to malloc.

note, the size of a single int is either 4 or 8 bytes depending on your compiler. but don't worry about that, what you need to worry about is malloc-ing enough ints to account for the number of rows AND the length of each row.

so not only will you need "nrows" to be the correct value, but you need the size of each row... think "ncols" or "rowlength"

a couple points while im here...

don't cast your malloc as an (int *) that's unnecessary and potentially buggy. it dates from the days before ANSI C and is not good practice.

and dont cast NULL as an (int *) either. NULL is already defined just fine, in the standard library.

as for "nrows" this is the number of rows that you should have already read from your text file before you allocate your matrix. which you havent

so make a while block that continues to get one line at a time as long as the result is not null. use "fgets"

see: http://www.cplusplus.com/reference/clibrary/cstdio/fgets/

consider this example:

char singleLine[MAXLINELEN];
nrows = 0;

while (fgets(singleLine, MAXLINELEN-1, infile) != NULL)
{
    nrows++;
    printf(line #%d: %s\n",nrows,singleLine);
}

how can you apply it to your code? and remember, you still need to know the size of each row, in additino to the number of rows, before you can correctly specify the size of your total array

Thank you so much for your help! I really appreciate it. Look out for me again, I'm sure I'll need help for something else. You explained it very well. Thank you so, so, so much again! Have a fabulous night!

:o)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.