我已经写了以下功能:
/*!
* @brief Loads the filecontent from the whitelist into an array of structs
* @param char *conffile - The whitelist file
* @param struct whitelist *confstruct - The array of structs
* @param int conflines - How many lines are in the given file
* @return int - 0 on succes, 1 on error
*/
int load_whitelist(char *conffile, struct s_whitelist *confstruct, int conflines)
{
FILE *infile;
if((infile = fopen(conffile, "r")) == NULL) // Try to open given file
{
fprintf(stderr, "File error!"); // Error on failure
return 1;
}
char line[256];
char *buffer;
char **linebuffer;
int i = 0;
while(i <= conflines) // As long as there are lines in the file (except comments starting with #)
{
confstruct[i].name = malloc(256); // Allocate memory for the regarding part of the struct
confstruct[i].gateway = malloc(256);
confstruct[i].user = malloc(256);
confstruct[i].pass = malloc(256);
confstruct[i].cert = malloc(256);
while(fgets(line, sizeof(line), infile) != NULL) // Get lines until the end of the file is reached
{
if(line[0] != '#') // If a line doesn't contain a '#'
{
linebuffer = malloc(count_string(line," ")+1); // Allocate memory in the linebuffer for every found blank
for(int y = 0; y <= count_string(line," "); y++) // For every found blank
{
linebuffer[y] = malloc(256); // allocate memory in the array
}
printf("%i| %s\n",count_string(line," "),line);
}
}
i++;
}
for(int y = 0; y <= count_string(line," "); y++)
{
free(linebuffer[y]);
}
free(linebuffer); // <---- That's the culprint
return 0;
}
Everthing工作,除了最后一个免费(linebuffer)。即使我在示例中找到了这种方式,我也尝试过不同的位置,不同类型的指针。那么,这次有人知道我做错了什么吗? 非常感谢您的帮助!