getc() 函数在 stdio.h 头文件中定义。它有助于从参数中指定的流中获取下一个字符。它还会推进流的位置指示符。
int getc(FILE *stream); #where stream should be a file pointer
getc() 函数接受一个参数,即要执行操作的 流。
| 参数 | 描述 | 必需/可选 |
|---|---|---|
| stream | 指向标识流的 FILE 对象的指针。 | 必需 |
在 getc() 函数中,字符值作为转换为 int 的无符号字符返回,或者在文件结束或错误时返回 EOF。
| 输入 | 返回值 |
|---|---|
| 成功时 | 返回下一个请求的对象 |
| 错误或文件结束时 | 整数值 |
#include <stdio.h>
int main()
{
char chr;
printf("Enter the character: ");
chr = getc(stdin);
printf("Your entered character is: ");
putc(chr, stdout);
return(0);
}
输出
Enter the character: P Your entered character is: P
#include <stdio.h>
int main (){
char chr;
FILE *pnt;
if (pnt = fopen("mytest.c", "r"))
{
chr = getc(pnt);
while (chr != EOF)
{
putc(chr, stdout);
chr = getc(pnt);
}
fclose(pnt);
return 0;
}
return 1;
}
输出
/* mytest.c file content */ Hi, How are you?