简体   繁体   English

fwrite C编程

[英]fwrite C programming

HI...i want to write something like this in a file, using fwrite 嗨...我想用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吗?

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

fwrite is designed mainly for writing raw binary data into a file, not text output. fwrite主要用于将原始二进制数据写入文件,而不是文本输出。 For text output it's more natural to use fprintf , fputs , fputc etc. 对于文本输出,使用fprintffputsfputc等更为自然。


If you really need fwrite , you have to separate the name part out, like: 如果确实需要fwrite ,则必须将name部分分开,例如:

fwrite("name is ", 1, 8, fp);
fwrite(name, 1, strlen(name), fp);
fwrite("\n", 1, 1, fp);

Better yet, at the bash prompt, do 更好的是,在bash提示下,执行

$ man fwrite

If on Windows, or a system without manpages installed, point a browser at http://linuxmanpages.com/ 如果在Windows或未安装手册页的系统上,请将浏览器指向http://linuxmanpages.com/

Seriously, the sooner you get familiar with manpages, the easier learning C will become. 认真地说,您越早熟悉手册页,学习C就越容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM