fputc 与fputs函数

1)int fputc(int ch,FILE *fp);
功能:向指定文件写入一个字符
参数:
    ch:字符
    fp:文件指针
返回值:
    成功:返回写入的字符
    失败:返回EOF
    
2)int fputs( char *str, FILE *fp );
功能:向指定文件写入字符串
参数:
    str:字符串
    fp:文件指针
返回值:
    成功:返回非负数
    失败:返回EOF

3)例程

    fputc函数

函数功能:向D:\\demo.txt写入键盘输入的字符

#include<stdio.h>
int main(){
    FILE *fp;
    char ch;
    //判断文件是否成功打开
    if( (fp=fopen("D:\\demo.txt","w+")) == NULL ){
        puts("Fail to open file!");
        return -1;
    }
    printf("Input a string:\n");
    //每次从键盘读取一个字符并写入文件
    while ( (ch=getchar()) != '\n' ){
        fputc(ch,fp);
    }
    fclose(fp);
    return 0;
}

 fputs函数

函数功能:将输入的字符串追加到D:\\demo.txt 中

#include<stdio.h>
int main(){
    FILE *fp;
    char str[102] = {0}, strTemp[100];
    if( (fp=fopen("D:\\demo.txt", "a+")) == NULL ){
        puts("Fail to open file!");
        return -1;
    }
    printf("Input a string:");
    gets(strTemp);
    strcat(str, "\n");
    strcat(str, strTemp);
    fputs(str, fp);
    fclose(fp);
    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值