网站建设资讯

NEWS

网站建设资讯

c语言fputc函数说明 fputs函数和fputc

C语言程序关于fputc函数

从你的程序看:num为1,而写入文本文件时以ASCII存储的自然也就不是想要的数值了。你如果想把输入的数值写入file.txt可以用下面的程序试试。

成都创新互联专注于栾川网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供栾川营销型网站建设,栾川网站制作、栾川网页设计、栾川网站官网定制、微信小程序开发服务,打造栾川网络公司原创品牌,更为您提供栾川网站排名全网营销落地服务。

#includestdio.h

#includestdlib.h

int num=0;

int main()

{

void writetofile();

printf("请输入一个数字:");

scanf("%d",num);

printf("结果为:%d\n",num);//输出整型num

num+=48;//把num加上48转换成ASCII 比如 num=0 则0的ASCII为48

writetofile();

return 0;

}

void writetofile()

{

FILE *fp;

fp=fopen("file.txt","w+");

fputc(num,fp);

fclose(fp);

}

C语言关于fputc函数的使用。

就是输入一个打出一个。putchar()括号里面的应该填一个字符或者字符数组名,比如a[0]

而10就是换行的ASCII码就是换行。相当于“\n“

]

函数fputc()的功能是什么?

函数fputc()的功能:将字符ch输出到fp所指向的文件中.若成功则返回该字符;否则返回EOF

c语言,关于fputc函数

这个问题是读和写的切换问题,见下:

摘自英文MSDN

When the "r+" access type is specified, both reading and writing are allowed. (The file is said to be open for "update".) However, when you switch from reading to writing, the input operation must encounter an EOF marker. If there is no EOF, you must use an intervening call to a file-positioning function. The file-positioning functions are fsetpos, fseek, and rewind. When you switch from writing to reading, you must use an intervening call to either fflush or to a file-positioning function.

当fopen_s的访问权限是"r+"时,可以对指定文件进行读和写的操作(该文件被打开用来“更新”)。但是,当从读操作换成写操作时,输入操作必须遇到一个EOF标识符(也就是说,必须全部读取完毕后才能写)。如果不想全部读取完毕后再能写,就必须使用一个设置文件位置函数作为中介。设置文件位置的函数可以是fsetpos()、fseek()和rewind()。当从写操作换成读操作时,必须调用fflush()函数或者设置文件位置函数作为中介。

请采纳QQQ


网站名称:c语言fputc函数说明 fputs函数和fputc
网站网址:http://cdweb.net/article/hhdode.html