函式名: getc
功 能:從流中取字元
用 法: int getc(FILE *stream);
//read the next character from stream and return it as an unsigned char cast to a int ,or EOF on end of file or error.
從檔案指針stream指向的檔案流中讀取一個字元,並把它作為函式值返回給整型變數ch;
如果讀取失敗或者到了檔案結束標誌返回EOF(-1)
基本介紹
- 外文名:getc
- 功 能:從流中取字元
- 注意:用時不能將其做為函式指針傳
- 程式例一:把檔案中的內容原樣輸出到螢幕上
功能
用法
程式例一
#include<stdio.h>#include<stdlib.h>int main(){FILE *fp;char ch;if((fp = fopen("D:\\file_a.dat" , "w")) == NULL){printf("Cannot open this file!\n");exit(0);}ch = getchar();for( ; ch != '@' ; )/*創建檔案並由用戶輸入字元,以@為輸入結束標誌*/{fputc(ch , fp);ch = getchar();}fclose(fp);if((fp = fopen("D:\\file_a.dat" , "r")) == NULL){printf("Cannot open this file!\n");exit(0);}ch = fgetc(fp);for( ; ch != EOF ; ){putchar(ch);ch = fgetc(fp);}fclose(fp);}
程式例二
#include<stdio.h>int main(void){char ch;printf("Input a character:");/*read a character from the standard input stream*/ch=getc(stdin);printf("The character input was:'%c'\n" , ch);return 0;}