用fputc()函数以字符串形式写入字符到磁盘文件

原创
2016/04/10 22:16
阅读数 709
#include <stdio.h>
#include <stdlib.h>

int main(){
  FILE *fp;
  char ch;

  if((fp=fopen("testfile", "a")) == NULL){
    fprintf(stderr, "Error opening file.\n",fp);
    exit(1);
  }

  printf("Input a string (Ctrl+D to exit):\n");
  while((ch=getchar()) != EOF){
    fputc(ch, fp);
  }

  if(ferror(fp)){
    puts("something wrong to read file\n");
  }else{
    puts("write file success!\n");
  }

  fclose(fp);
  printf("\n");
  return 0;
}


展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
1
分享
返回顶部
顶部