I have a line-oriented file-format, and use get line to retrieve the data. However, the (string) data can not have the newline character. I am trying to write a function that doesn't return a string with the newline, but I'm having trouble:
Code:
char * getlineNoEndl(FILE *file)
{
char *str = NULL;
int strsize = 0;
getline(&str, &strsize, file);
if (strsize >= 2)
{
/* shrink string */
str = realloc(str, (--strsize) * sizeof(char));
str[strsize - 1] = '\0';
}
else
{
free(str);
str = malloc(sizeof(char));
*str = '\0';
}
return str;
}
The function currently appears to still return a string with a newline in it.