开发者

fwrite C programming

开发者 https://www.devze.com 2022-12-28 14:47 出处:网络
HI...i want to write something like this in a file, using fwrite fwrite(\"name is %s\\n\",name, 60, fp);

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开发者_运维百科?


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);


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号