c語言getc函數
C語言中的getc()函數 (getc() function in C)
Prototype:
原型:
int getc(FILE *filename);
Parameters:
參數:
FILE *filename
Return type: int
返回類型: int
Use of function:
使用功能:
In the file handling, through the getc() function we take the next character from the input file stream and increment the file position pointer. The prototype of the function getc() is:
在文件處理中,通過getc()函數,我們從輸入文件流中獲取下一個字符,并遞增文件位置指針。 函數getc()的原型為:
int getc(FILE *filename);
It returns an integer value which is conversion of an unsigned char. It also returns EOF which itself is also an integer value. Whenever there is a binary file, check for EOF with the function feof().
它返回一個整數值,該值是無符號char的轉換。 它還返回EOF ,它本身也是一個整數值。 只要有二進制文件,請使用feof()函數檢查EOF 。
C語言中的getc()示例 (getc() example in C)
#include <stdio.h>
#include <stdlib.h>
int main(){
//Initialize the file pointer
FILE *f;
char ch;
//Create the file for write operation
f=fopen("includehelp.txt","w");
printf("Enter five character\n");
for(int i=0;i<5;i++){
//take the characters from the users
scanf("%c",&ch);
//write back to the file
putc(ch,f);
//clear the stdin stream buffer
fflush(stdin);
}
//close the file after write operation is over
fclose(f);
//open a file
f=fopen("includehelp.txt","r");
printf("Write operation is over and file is ready for read operation\n");
printf("\n...............print the characters..............\n\n");
while(!feof(f)){
//takes the characters in the character array
ch=getc(f);
//and print the characters
printf("%c\n",ch);
}
fclose(f);
return 0;
}
Output
輸出量
翻譯自: https://www.includehelp.com/c-programs/getc-function-in-c-language-with-example.aspx
c語言getc函數