-
fputc
鎖定
- 中文名
- fputc
- 函數名稱
- 寫字符文件函數fputc()
- 函數格式
- int fputc
- 參數解釋
- fp為文件指針,執行fopen時獲得
fputc函數介紹
函數名稱:寫入字符文件函數fputc()
函數格式:fputc (char c, File *fp)
參數解釋:fp為文件指針,它的值是執行fopen()打開文件時獲得的。
c為輸入的字符量。
雖然函數被定義為整型數,但僅用其低八位。
返回值:在正常調用情況下,函數返回寫入文件的字符的ASCII碼值,出錯時,返回EOF(-1)。當正確寫入一個字符或一個字節的數據後,文件內部寫指針會自動後移一個字節的位置。EOF是在頭文件 stdio.h中定義的宏。
fputc程序範例
fputc程序例一
#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++; } return 0; }
fputc程序例二
#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); }
fputc程序例三
#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); return 0; }
備註:putc()功能與用法與之完全相同。