C++ 输入输出,IO

1.printf()从右到左计算输出.
int i = 3;
printf("%d %d", ++i, ++i);
结果:5 5
2.fputc()

函数名称:写字符文件函数fputc()
函数格式:int fputc (char c, File *fp)
参数解释:fp为文件指针,它的值是执行fopen()打开文件时获得的。

程序例一

#include <stdio.h>
#include <string.h>
int main(void)
{
char msg[]="Hello world";
int i=0;
while(msg[i]&&(i<strlen(msg)))
{
fputc(msg[i],stdout);//输出到控制台
i++;
}
return0;
}

程序例二

#include <stdio.h>
#include <stdlib.h>
void main()
{
FILE*fpout;
char ch;
if((fpout=fopen("file_a.dat","w"))==NULL)
{
printf("Error!\n");
exit;
}
ch=getchar();
for(;ch!='#';)
{
fputc(ch,fpout);
ch=getchar();//不能仅写getchar();
}
fclose(fpout);
}

程序例三

#include <stdio.h>
#include <string.h>
int main()
{
FILE*f;
char*s="Hey,Buddy!";
int i;
f=fopen("myFile.txt","w");
for(i=0;i<strlen(s);i++)
fputc(s[i],f);
fclose(f);
return0;
}

 



posted @ 2016-03-23 20:05  YuntaoWu  阅读(247)  评论(0编辑  收藏  举报