c - 在C中的字符串矩阵上重新分配空间
问题描述
#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;
}
推荐阅读
- android-studio - 为什么请求 http 在 Kotlin 中不起作用?
- java - 如何解决“原始使用参数化类'Comparable'”警告?
- python - Python 变量的值不会在函数之外更改
- go - 分配 C 内存来保存 Go 结构是 cgo 支持的用例吗?
- javascript - 尝试将字符串传递给构造函数
- common-lisp - uiop:launch-program 和 nodejs
- python - 如何仅将列表中的数字与下一个数字(邻居)进行比较?
- git - 如何在github上正确发布?
- javascript - 按类过滤 div
- r - 为列表中的每个数据框添加后缀?