-
fwrite question
I have a string
which i declare like this :
char string[60]="Hello";
fwrite(string, sizeof(string), 1, file);
the problem is, when writing to the file, it not only writes hello, but it also includes the remaining elements which have not been initialised...which leaves me with 50 over other spaces in the file...
i know u would say why not just do
char string[5]="Hello";
but i just want to allocate more in the buffer so i can add more characters in the future...
-
Instead of writing the whole size of the buffer, i.e. 60 chars, to the file, you could consider writing only 5 characters to the file. To make it a bit more general, you could first use strlen() to get the length of the string being stored in your buffer and then use this value for fwrite().
-
Or you could use fprintf.
>>it also includes the remaining elements which have not been initialised<<
Not so, they have been initialised to \0.
-
okok, i'll give it a try...