Does anyone see anything wrong with these simple functions?
Basically, the "get_field" gets a word from the file of unknown length and "add_char" will add the character to the string.
The problem lies somewhere in the "add_char" function because the memory is never realloc'd. I don't get any error messages--the word is just blank when printed. :-(
Code:
char *add_char(char *string,char character)
{
char *tmp=realloc(string,sizeof(string)+sizeof(char));
if(tmp!=NULL)
{
string=tmp;
}
string[sizeof(string)/sizeof(char)]=character;
return string;
}
char *get_field(FILE *inputfile)
{
char character;
char *field;
field=(char*)malloc(sizeof(char)*2);
do
{
character=getc(inputfile);
field=add_char(field,character);
}
while(character!='\0');
field[sizeof(field)]='\0';
return field;
}