C语言 关于fgetc 函数

Python09

C语言 关于fgetc 函数,第1张

fgetc函数的功能:从文件指针stream指向的文件中读取一个字符,读取一个字节后,光标位置后移一个字节。

格式:int fgetc(FILE *stream)

说明:这个函数的返回值,是返回所读取的一个字节。如果读到文件末尾或者读取出错时返回EOF。

示例:

#include <string.h>

#include <stdio.h>

#include <conio.h>

int main(void)

{

    FILE *stream

    char string[ ] = "This is a test"

    int ch

     

    /* open a file for update */

    stream = fopen("DUMMY.FIL", "w+")

     

    /* write a string into the file */

    fwrite(string, strlen(string), 1, stream)

    /* seek to the beginning of the file */

    fseek(stream, 0, SEEK_SET)

    do

    {

        /* read a char from the file */

        ch = fgetc(stream)

        /* display the character */

        putch(ch)

    } 

    while (ch != EOF)

    fclose(stream)

    return 0

}

c语言中fgetc和getc与fputc和putc的区别如下:

fgetc是从数据流中取一个字符,比如从一个打开的文件中取一个字符

fputc是将一个字符送入到一个数据流中,比如王一个打开的文件中写入一个字符

getc是从键盘中获取一个字符

putc是往屏幕送一个字符

1、fgets和fgetc都是文件函数中的输入函数。其中第fgets是输入一个字符串,而fgetc则是输入一个字符。

2、例如:

#include <string.h>

#include <stdio.h>

int main(void)

{

FILE *stream

char string[100],c

stream = fopen("fan.txt", "r+") /* 打开一个文本*/

fgets(string,99, stream) /* 读取文本中第一行 */

printf("%s", string)/* 在控制台显示该行文字 */

c = fgetc(stream)//读取文本第二行第一个字符

putchar(c)//在控制台显示该字符

fclose(stream)//关闭文件

return 0

}