I need to know how to read a string from a binary file using fread ().
Thing is, as I understand, if I want to copy the value of some string to a new string declared like this:
char *string;
I need to calculate first the length of the other string using strlen (), use that value to reserve memory for my new string with malloc, and then copy the value o the other string to my new string using strcpy ()
Kinda like this:
newLength = strlen ( otherString ) + 1;
string = malloc ( sizeof ( char ) * newLength );
if ( string == NULL ) {
return ( FALSE )
}
But what happens if I am reading data from a binary file, and I am trying to read a string from said file but I don't know beforehand its length, therefore I can't use malloc to reserve memory for said string?
Would this work, even though I haven't reserved memory for the string yet (I am not so convinced)?:
fread ( string, sizeof ( char ), strlen ( string ), currentFile );
I am kinda stuck now. Hopefully you guys can shed me some light and guide me a bit.