将 .txt 文件传输到 malloc 分配的动态内存中
Transfer .txt file to dynamic memory allocated by malloc
所以,我已经在 Whosebug 周围搜索了一段时间,但是 none 半相关的帖子是我的解决方案,所以我希望任何人都能提供帮助:)
所以我想做的是传输一个 .txt 文件,其中包含以下内容:
1 5 8
2 4 6 9
5 7
(每个例子)
那么,我想知道的是:
如何使用 malloc 将 .txt 传输到分配的内存;
我必须分别读取每一行并提取数字,然后与.txt文件中ID与数字对应的结构的内容进行比较。
如果我将.txt的内容转移到数组中(使用malloc),它是否仍然在数组上有'\n',所以我可以区分行?
由于值从 1
变为 9
,您可以使用 0
作为每一行的标记,这样您就不需要预测或存储数字每行中的数字,你只需存储所有出现的数字,并在末尾添加一个 0
,这将帮助你知道数字在哪里结束,这与 c 处理字符串的方式相同。
下面的代码完成了我所描述的
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int
appendvalue(int ***values, int row, int column, int value)
{
void *pointer;
pointer = realloc(values[0][row], (1 + column) * sizeof(int));
if (pointer == NULL)
{
fprintf(stderr, "warning: allocating memory\n");
return 0;
}
values[0][row] = pointer;
values[0][row][column] = value;
return 1 + column;
}
int
main(void)
{
FILE *file;
char buffer[100];
char *line;
int **values;
int row;
file = fopen("input.txt", "r");
if (file == NULL)
{
fprintf(stderr, "error opening the file\n");
return -1;
}
values = NULL;
row = 0;
while ((line = fgets(buffer, sizeof(buffer), file)) != NULL)
{
void *pointer;
size_t column;
while (isspace(*line) != 0)
line++;
if (*line == '[=10=]') /* empty line -- skip */
continue;
pointer = realloc(values, (row + 1) * sizeof(int *));
if (pointer == NULL)
{
fprintf(stderr, "error allocating memory\n");
return -1;
}
values = pointer;
values[row] = NULL;
column = 0;
while ((*line != '[=10=]') && (*line != '\n'))
{
if (isspace(*line) == 0)
column = appendvalue(&values, row, column, *line - '0');
++line;
}
column = appendvalue(&values, row, column, 0);
row += 1;
}
/* let's print each row to check */
for (--row ; row >= 0 ; --row)
{
size_t index;
for (index = 0 ; values[row][index] != 0 ; ++index)
{
printf("%d, ", values[row][index]);
}
printf("\n");
free(values[row]);
}
free(values);
return 0;
}
所以,我已经在 Whosebug 周围搜索了一段时间,但是 none 半相关的帖子是我的解决方案,所以我希望任何人都能提供帮助:)
所以我想做的是传输一个 .txt 文件,其中包含以下内容:
1 5 8
2 4 6 9
5 7
(每个例子)
那么,我想知道的是:
如何使用 malloc 将 .txt 传输到分配的内存;
我必须分别读取每一行并提取数字,然后与.txt文件中ID与数字对应的结构的内容进行比较。
如果我将.txt的内容转移到数组中(使用malloc),它是否仍然在数组上有'\n',所以我可以区分行?
由于值从 1
变为 9
,您可以使用 0
作为每一行的标记,这样您就不需要预测或存储数字每行中的数字,你只需存储所有出现的数字,并在末尾添加一个 0
,这将帮助你知道数字在哪里结束,这与 c 处理字符串的方式相同。
下面的代码完成了我所描述的
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int
appendvalue(int ***values, int row, int column, int value)
{
void *pointer;
pointer = realloc(values[0][row], (1 + column) * sizeof(int));
if (pointer == NULL)
{
fprintf(stderr, "warning: allocating memory\n");
return 0;
}
values[0][row] = pointer;
values[0][row][column] = value;
return 1 + column;
}
int
main(void)
{
FILE *file;
char buffer[100];
char *line;
int **values;
int row;
file = fopen("input.txt", "r");
if (file == NULL)
{
fprintf(stderr, "error opening the file\n");
return -1;
}
values = NULL;
row = 0;
while ((line = fgets(buffer, sizeof(buffer), file)) != NULL)
{
void *pointer;
size_t column;
while (isspace(*line) != 0)
line++;
if (*line == '[=10=]') /* empty line -- skip */
continue;
pointer = realloc(values, (row + 1) * sizeof(int *));
if (pointer == NULL)
{
fprintf(stderr, "error allocating memory\n");
return -1;
}
values = pointer;
values[row] = NULL;
column = 0;
while ((*line != '[=10=]') && (*line != '\n'))
{
if (isspace(*line) == 0)
column = appendvalue(&values, row, column, *line - '0');
++line;
}
column = appendvalue(&values, row, column, 0);
row += 1;
}
/* let's print each row to check */
for (--row ; row >= 0 ; --row)
{
size_t index;
for (index = 0 ; values[row][index] != 0 ; ++index)
{
printf("%d, ", values[row][index]);
}
printf("\n");
free(values[row]);
}
free(values);
return 0;
}