我在char *内容中获取正确数据时遇到问题。我想从文件中读取其内容,用它填充我的缓冲区,并能够将char **内容与缓冲区的地址相关联。然后从主要的char *内容中获取数据......到目前为止我做错了什么?
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
void load(FILE* file, char** content, size_t* length);
int main()
{
FILE* file = fopen("file.txt", "r");
char* content;
size_t length = 0;
load(file, &content, &length);
printf("%s\n", content);
printf("%lu\n", length);
}
void load(FILE* file, char** content, size_t* length)
{
char buffer[30];
fgets(buffer, 30, file);
*content = buffer;
*length = strlen(buffer);
}
答案 0 :(得分:0)
您的加载函数应如下所示:
void load(FILE* file, char** content, size_t* length)
{
char *buffer;
buffer = (char*)malloc(30*sizeof(char));
fgets(buffer, 30, file);
*content = buffer;
*length = strlen(buffer);
}