gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:38:13 27 4
gpt4 key购买 nike

#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;
}

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

3333333333 date1 u
2222222222 date2 e

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

在这个文件中

3333333333 date1 u
2222222222 date2 e
8888888888 date3 e

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

最佳答案

您需要为每一行分配空间。

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;
}

关于c - 在 C 中的字符串矩阵上重新分配空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53599694/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com