c Language File Input/Output and Dynamic Assignment

Asked 2 years ago, Updated 2 years ago, 36 views

Question Title:

txt file input output program

Question summary:

The problem is that I understood to receive a when entering dynamic allocation

I want to receive b, but it is saved as an array that I put in a. And I can't read the text below and it's printed as garbage.

Code

///*

//#include

//#include

//

//

//void print_matrix(int row, int col, int **mat)

//{

// int i, j;

// for(i = 0; i < row; i++) // Increase row

// {

// for(j = 0; j < col; j++) // Increase heat

// {

// printf("%3d", mat[i][j]); // output to screen

// }

// printf("\n"); // Output open characters on the screen

// }

//}

//

//int main(void)

//{

//

// file *fp;

// int a_row = 0, a_col = 0,bh =0 ,bw=0;

// int a_matrix, **b_matrix,c_matrix;

// int i,j;

// char num,n1,n2,n3,n4,n5,n6;

//

// fp = fopen("data.txt", "r");

// if(fp==null)

// {

// printf ("The file did not open".\n");

// return 0;

// }

//

// //while(!feof(fp))

// //{

// fscanf(fp, "%c%d%d",&num, &a_row, &a_col);

// //fscanf(fp, "%c%d%d",&n1, &bh, &bw);

//

// //Assignment

// a_matrix = (int**) malloc(sizeof(int *) * a_row);

// for(i = 0; i < a_row; i++)

// {

// a_matrix[i] = (int*) malloc(sizeof(int ) * a_col);

// }

// b_matrix = (int**) malloc(sizeof(int *) * a_row);

// for(i = 0; i < a_row; i++)

// {

// b_matrix[i] = (int*) malloc(sizeof(int ) * a_col);

// }

// /c_matrix = (int*) malloc(sizeof(int *) * a_row);

// for(i = 0; i < a_row; i++)

// {

// c_matrix[i] = (int*) malloc(sizeof(int ) * a_col);

// }*/

// // Save

// for(i = 0; i < a_row; i++)

// {

// for(j = 0; j < a_col; j++)

// {

// fscanf(fp, "%d", &a_matrix[i][j]);

//

// }

// }

// /*

// for(i = 0; i < a_row; i++)

// {

// for(j = 0; j < a_col; j++)

// {

// fscanf(fp, "%d", &b_matrix[i][j]);

// }

// }*/

// /*for(i = 0; i < a_row; i++)

// {

// for(j = 0; j < a_col; j++)

// {

// fscanf(fp, "%d", &c_matrix[i][j]);

// }

// }*/

// //Output

// /*printf("%d\n", a_row);

// printf("%c\n", num);

// printf("===%c matrix ===\n",num);

// print_matrix(a_row, a_col, a_matrix);*/

// //}

// printf("===%c matrix ===\n",num);

// print_matrix(a_row, a_col, a_matrix);

// for(i = 0; i < a_row; i++) free(a_matrix[i]);

// free(a_matrix);

//

// for(i = 0; i < a_col; i++) free(b_matrix[i]);

// free(b_matrix);

//

// return 0;

//}*/

c

2022-09-22 21:35

1 Answers

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

void print_matrix(int row, int col, int **mat)
{
    int i, j;
    For(i = 0; i < row; i++) //increase the row
    {
        For(j = 0; j < col; j++) // Increasing heat
            printf("%3d", mat[i][j]); // output to screen

        printf("\n"); // Output open characters on screen
    }
}

int main(void)
{
    FILE *fp;
    int i, j, index = 0;
    int row[3] = {0};
    int col[3] = {0};
    int **matrix[3];
    char matrix_name[3] = {0};

    fp = fopen("data.txt", "r");

    if(fp == NULL)
        error("The file was not opened.\n");

//Assign and enter
    while(!feof(fp))
    {
        fscanf(fp, " %c %d %d", &matrix_name[index], &row[index], &col[index]);

        matrix[index] = (int**) malloc(sizeof(int *) * row[index]);
        for(i = 0; i < row[index]; i++)
            matrix[index][i] = (int*) malloc(sizeof(int ) * col[index]);

        for(i = 0; i < row[index]; i++)
            for(j = 0; j < col[index]; j++)
                fscanf(fp, "%d", &matrix[index][i][j]);

        index++;
    }

//Output
    for(i = 0 ; i < index ; i++)
    {
        printf("===%c matrix ===\n", matrix_name[i]);
        print_matrix(row[i], col[i], matrix[i]);
    }

//release
    for(i = 0 ; i < index ; i++)
    {
        for(j = 0; j < row[i]; j++)
            free(matrix[i][j]);
        free(matrix[i]);
    }
    return 0;
}

Please read it and ask me what you don't understand.


2022-09-22 21:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.