Originally Posted by
laserlight
Provide the smallest and simplest compilable program that demonstrates the error.
Code:
typedef String char;
String* String_new(){
String *self=calloc(1, sizeof(char));
self[0]='\0';
return self;
}
void String_addChar(String *self, char ch){
if(ch=='\0'){//If the character is NULL
return;//Exit function
}
int len=strlen(self);
self=realloc(self, sizeof(char)*len+2);
char buffer[]={ch,'\0'};
strcat(self,buffer);
}
String* String_readFile(FILE* file){
String *buffer=String_new();
char current=fgetc(file);
while(current!=EOF){
String_addChar(buffer,current);
current=fgetc(file);
}
return buffer;
}
FILE* file=fopen("hello.txt","r");
String *f=String_readFile(file);
fclose(file);
free(f);