简体   繁体   中英

Reading in a text file with 5 lines to be filled into a 2D array in C

I am facing a problem with reading in a text file, using that information to form a 2D array which will print out to be a matrix.

Text file with these values:

4
9 12 13 1
7 3 6 9
8 4 2 1
10 5 15 3

The very first integer (4) will be used to determine the rows and columns of the 2D array, ex.

int row = 4;
int col = 4;
int matrix[row][col]; //will be a 4x4 matrix

How can I use the first line of the code (value: 4) to get the dimensions of the array but then store the remaining values into the array?

I've tried to do something like this:

#define ARRAYSIZE 10000
char *matDimen;
char *tokenArray;
char buffer[ARRAYSIZE];


FILE *file = fopen("textfile.txt", "r");

while (fgets(buffer, sizeof buffer, file) != NULL)
{
    if (row == 0) //Take the first value read to determine the dimension of the array
    {
        matDimen = strtok(buffer, " "); //Using strtok to read in the first value as a string
        row = atol(matDimen); //Converting the string into an int
        col = row; //Because rows and columns are going to be the same, initialize column to row

        if (row < 1 || 10 < row)
        {
            if (col < 1 || 10 < row)
                {
                    printf("Matrix must be within 2x2 and 9x9.\n");
                    exit(0);
                }
        }
    }
    for (i = 0; i < row; i++) //Ideally, assigns values from the text file into an index of the array
    {
        for (j = 0; j < col; j++)
        {
            tokenArray = strtok(NULL, " ");
            //strcpy(matrix[i][j], tokenArray); Program crashes here
            //printf("%s ", matrix[i][j]);
        }

    if (j == col) //Formats the matrix into a 4x4 square, in this scenario
    {
        printf("\n");
    }
}
fclose(file);

What I want is:

1) Read the very first value to determine the dimension of the 2D array

2) Go to the next line

3) Read the next values one by one and store them into the array

4) Print it out.

How can I go about doing this?

Thank you for reading.

An array is basically a pointer to a bunch of values, so consider an array arr of type int:

arr[0];

Is the same thing as

*arr;

And

arr[2];

Is the same as

*(arr+2);

Thus, if you have a 2D array of int, you actually have a pointer to a pointer of type int. Using malloc() you can then make the array dynamic in size: (Suppose we read the number of rows to a var called 'rows')

int** arr = malloc(sizeof(int[4])*rows);

arr now is a pointer to an array of 4 integers, that can be accessed as usual:

arr[row][col]

Hope that clears it up for you, Google "dynamic 2d array c" for more info :)

You can do things one at a time instead of one loop

fgets(buf, sizeof buf, file);
if (!buf) printf("error 1...\n");
row = atoi( buf ); 
if ( row < 1 || row > 10 || col < 1 || col > 10 )  printf("error 2...\n");

int r = 0;
while (fgets(buf, sizeof buf, file) )
{
   char *p = strtok( buf, " " );
   for (int column = 0; column < row; column++)
   {
      if (!p) break;//error
      matrix[r][column] = atoi(p);
      p = strtok (NULL, " ");
   }
   r++;
}

Also there is a problem with arrays as mentioned in previous answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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