why are the output files different when I use fwrite in another function VERSUS fwrite in the same function?
output1.txt contains garbage value like Ê, which is NOT corr
In the writeData function, in your call to fwrite:
fwrite(&buf, sizeof(char), strlen(buf), fp1);
the variable buf is a pointer to the first character in the string to write. It's of typechar *. However the expression &buf is a pointer to the variable buf, its type is char **. It's not the same data.
It works if buf is an array because both then buf (which is really &buf[0]) and &buf points to the same location. They are still different types though.
For example with
char buf[2];
then buf decays to a pointer to the arrays first element (i.e. &buf[0]) and is of type char *. The expression &buf is a pointer to the array and is of type char (*)[2].
Somewhat graphically
+--------+--------+ | buf[0] | buf[1] | +--------+--------+ ^ | &buf[0] | &buf
Two pointers to the same location, but different types and different semantic meanings.