在C中的字符串矩阵上重新分配空间

时间:2018-12-03 18:28:04

标签: c pointers matrix

select listagg('AA.' || col1 || ' BB_' || col1, ', ') within group (order by col1)
from t;

我正在尝试创建一个字符串矩阵,每当文件输入中有新行时,它将增加行大小。该文件将如下所示。

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

#define MAX_STRING_LENGTH 100

int main(int argc, char const *argv[])
{
    FILE* file;
    char ***myTable;
    /* I want to declare a matrix which will contain a string in each cell */
    int end = 1;
    int row = 0;

    /* space declaration */

    myTable = (char ***) malloc(sizeof(char**));   

    myTable[0] = (char **)malloc(3 * sizeof(char*));
    for(int j = 0; j < 3; j++)
        myTable[0][j] = (char *)malloc(MAX_STRING_LENGTH * sizeof(char));
    file = fopen(argv[1], "r");

    while(end) {
        //filling the matrix from the file no matter how many row
        //the file has
        for(int j = 0; j < 3; j++) {
            fscanf(file, "%s", myTable[row][j]);
        }
        //adding one row every time i retrive the data

        myTable = (char***) realloc(myTable, sizeof(**myTable) * (row+1));
        row++;
        if(getc(file) == EOF) end = 0;
    }

    fclose(file);

    for(int rows = 0; rows < row; rows++) {
        for(int col = 0; col < 3; col++)
            printf("numero: %s ", myTable[rows][col]);
        printf("\n");
    }
    return 0;
}

在这种情况下,程序内部的矩阵将具有2行和3列。

在此文件中

3333333333 date1 u
2222222222 date2 e

在这种情况下,程序内部的矩阵将具有3行和3列;列将始终是固定的,但每次的行数必须增加一。

1 个答案:

答案 0 :(得分:1)

您需要为每个新行分配空间。

int main(int argc, char const *argv[])
{
    FILE* file;
    char ***myTable = NULL;
    /* I want to declare a matrix which will contain a string in each cell */
    int end = 1;
    int row = 0;

    file = fopen(argv[1], "r");

    while(end) {
        // TODO: Add NULL ptr check....
        // Allocate space here:
        // First, add a row
        myTable = realloc(myTable, sizeof(char**) * (row + 1));
        // Next, alloc 3 pointers in the new row
        myTable[row] = malloc(3 * sizeof(char*));
        // Then, alloc space for 3 strings
        for(int j = 0; j < 3; j++)
            myTable[row][j] = malloc(MAX_STRING_LENGTH * sizeof(char));

        //filling the matrix from the file no matter how many row
        //the file has
        for(int j = 0; j < 3; j++) {
            fscanf(file, "%s", myTable[row][j]);
        }
        row++;
        if(getc(file) == EOF) end = 0;
    }

    fclose(file);

    for(int rows = 0; rows < row; rows++) {
        for(int col = 0; col < 3; col++)
            printf("numero: %s ", myTable[rows][col]);
        printf("\n");
    }
    return 0;
}

注意:您可以通过创建一个结构来保存数据来简化操作:

struct my_data {
    char id[100];
    char name[100];
    char data[100];
};

然后简化为:

int main(int argc, char const *argv[])
{
    FILE* file;
    struct my_data * myTable = NULL;
    /* I want to declare a matrix which will contain a string in each cell */
    int end = 1;
    int row = 0;

    file = fopen(argv[1], "r");

    while(end) {
        // TODO: Add NULL ptr check....

        myTable = realloc(myTable, sizeof(struct my_data) * (row + 1));

        //filling the matrix from the file no matter how many row
        //the file has
        fscanf(file, "%s %s %s", myTable[row].id, myTable[row].name, myTable[row].data);
        // TODO: check the return value from fscanf

        row++;
        if(getc(file) == EOF) end = 0;
    }

    fclose(file);

    for(int rows = 0; rows < row; rows++) {
        printf("numero: %s %s %s\n", myTable[rows].id, myTable[rows].name, myTable[rows].data);
    }
    return 0;
}