I think I'm semi-close to a solution, but it's just not happening for me. I'm having trouble populating my dynamically allocated multidimensional array with data from a text file. Here's what I have:

#include <stdio.h>
#include <stdlib.h>
#define  MAXLINELEN 18

FILE  *getOpen(); 

int main()
{  
   
   FILE        *inFile;  // Declares inFile pointer as a FILE type.
   char        **matrix;
   int          nrows;   // number of rows.
   int          ncols;   // number of columns.
   int          i;
   int          row;
   int          col;
   char       singleLine[MAXLINELEN];
   
   
   // Open the text file containing the matrix.
   inFile = getOpen();

   // Read in each line of the matrix from the .txt file to obtain the
   // number of rows and columns so memory can be allocated for
   // the matrix.
   nrows = 0;
   ncols = 0;
   while (fgets(singleLine, MAXLINELEN - 1, inFile) != NULL)
   {
         nrows++;
         ncols++;
         printf("#%d: %s\n", nrows, singleLine);
         
   }
   
   
   matrix = malloc(nrows * sizeof(char *));
   if(matrix == NULL)
   {
        printf("Out of memory.\n");
        exit(1);
   }
   for(i = 0; i < nrows; i++)
   {
        matrix[i] = malloc(ncols * sizeof(char *));
        if(matrix[i] == NULL)
        {
             printf("Out of memory.\n");
             exit(1);
        }
   }
   printf("Memory allocated successfully!\n");
   
   // THE PROBLEM IS IN THESE NEXT STATEMENTS!!!!!
   for (row = 0; row < nrows; row++)
    {
        for (col = 0; col < ncols; col++)
        {
           while (fscanf(inFile, "%s %s\n", matrix) != EOF);
           // HOW DO I FILL THE MATRIX WITH THE DATA???!!!!!

           printf("%s %s\n", matrix);
           
        }
    }
   
   
   
   
   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);
}

I have tried scanf, fscanf, fgets, and fgetc. NOTHING IS WORKING!!!
PLEASE HELP!!!!!!!!!!!!!

Thanks!

Recommended Answers

All 9 Replies

Because you are expecting the language to do the work for you. When you read strings, you need to deal with the strings yourself. And since we have no idea what you are reading and trying to do, it's hard to point you in a direction.

You need to understand the commands you are using. Not just throw them in because you heard of them. Your fscanf() call is wrong.

Use fgets() and break the line up yourself.

the text file has this in in:

0 0 0 0 0 0 0 0
0 0 4 0 0 0 1 0
0 0 2 2 0 0 2 0
0 0 3 0 0 3 3 0
0 0 0 0 4 4 0 0
0 5 0 0 5 0 0 0
0 6 0 0 6 6 0 0
0 0 0 0 0 0 0 0

I understand how fgets works, I see it working in the read before allocating the memory. How do I use it to fill the matrix?

Can anyone help me? I've been working on this for like 2 weeks.

Will your input always be a square matrix ? Or the way you are calculating the number of columns is wrong.

Read a line using fgets, then copy that line character by character at the appropriate location in the matrix.

Will your input always be a square matrix ? Or the way you are calculating the number of columns is wrong.

Read a line using fgets, then copy that line character by character at the appropriate location in the matrix.

Yes, it will always be a square matrix. I had a feeling the way I was calculating the columns was wrong. How do I code what you just said? It makes sense. I'm having trouble with coding, and my teacher is no help.

If you want your array to hold strings, you'll need another dimension and need to allocate space for the strings. Or do you want it to be a 2D array of chars? Or ints?

If you want your array to hold strings, you'll need another dimension and need to allocate space for the strings. Or do you want it to be a 2D array of chars? Or ints?

2D array of chars

rewind(inFile); after you've got the rows and columns. Then read a character into the each of the matrix locations.

rewind(inFile);
   for (row = 0; row < nrows; row++)
    {
        for (col = 0; col < ncols; col++)
        {
           if (fscanf(inFile, "%c ", &matrix[row][col]) == 1)
           {
              printf("%c ", matrix[row][col]);
           }
        }
        putchar('\n');
    }
commented: wtg +2

rewind(inFile); after you've got the rows and columns. Then read a character into the each of the matrix locations.

rewind(inFile);
   for (row = 0; row < nrows; row++)
    {
        for (col = 0; col < ncols; col++)
        {
           if (fscanf(inFile, "%c ", &matrix[row][col]) == 1)
           {
              printf("%c ", matrix[row][col]);
           }
        }
        putchar('\n');
    }

ARE YOU KIDDING ME? Seriously, are you kidding me? FINALLY! I'm getting somewhere! I feel like crying right now, you have know idea. Thank you so much!

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.