tags:

views:

156

answers:

2

HI...i want to write something like this in a file, using fwrite

fwrite("name is %s\n",name, 60, fp);

but is not working, only write in the file the string. any idea?

+9  A: 

Do you mean fprintf?

fprintf(fp, "name is %s\n", name);

fwrite is designed mainly for writing raw binary data into a file, not text output. For text output it's more natural to use fprintf, fputs, fputc etc.


If you really need fwrite, you have to separate the name part out, like:

fwrite("name is ", 1, 8, fp);
fwrite(name, 1, strlen(name), fp);
fwrite("\n", 1, 1, fp);
KennyTM
Another method is to use `sprintf` to a buffer, then use `fwrite` to write the buffer.
Thomas Matthews
+4  A: 

Better yet, at the bash prompt, do

$ man fwrite

If on Windows, or a system without manpages installed, point a browser at http://linuxmanpages.com/

Seriously, the sooner you get familiar with manpages, the easier learning C will become.

Kevin Little