C 中的 getc() 函數
原型:
int getc(FILE *filename);
參數:
FILE *filename
返回類型:整型
函數的使用:
在文件處理中,我們通過 getc() 函數從輸入文件流中取出下一個字符並遞增文件位置指針。函數 getc() 的原型為:
int getc(FILE *filename);
它返回一個整數值,它是一個unsigned char
.它也返回EOF
它本身也是一個整數值。每當有二進製文件時,檢查EOF
與函數feof()
。
C 中的 getc() 示例
#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;
}
輸出

相關用法
- C語言 getchar()用法及代碼示例
- C語言 getch()用法及代碼示例
- C語言 getarcoords()用法及代碼示例
- C語言 getpixel()用法及代碼示例
- C語言 getx()用法及代碼示例
- C語言 getbkcolor()用法及代碼示例
- C語言 gets()用法及代碼示例
- C語言 getdate()、setdate()用法及代碼示例
- C語言 getmaxx()用法及代碼示例
- C語言 gety()用法及代碼示例
- C語言 getmaxy()用法及代碼示例
- C語言 getmaxcolor()用法及代碼示例
- C語言 grapherrormsg()用法及代碼示例
- C語言 fread()用法及代碼示例
- C語言 feof()用法及代碼示例
- C語言 imagesize()用法及代碼示例
- C語言 strcspn()用法及代碼示例
- C語言 setlinestyle()用法及代碼示例
- C語言 showbits()用法及代碼示例
- C語言 sprintf()用法及代碼示例
注:本文由純淨天空篩選整理自Souvik Saha大神的英文原創作品 getc() function in C language with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。