fgetc

来源:互联网 发布:软件规模度量单位 编辑:程序博客网 时间:2024/05/09 03:15


意为从文件指针stream指向的文件中读取一个字符,读取一个字节后,光标位置后移一个字节。
中文名
fgetc
概    述
意为从文件指针stream
功 能
从流中读取字符。
用    法
格式:int fgetc(FILE *s

目录

  1. 1功 能
  2. 2用法
  3. 3程序例
  1. 4Linux C
  2. 相关函数
  3. 表头文件
  4. 定义函数
  1. 函数说明
  2. 返回值
  3. 范例

功 能

编辑
从流中读取字符。

用法

编辑
格式:int fgetc(FILE *stream);
这个函数的返回值,是返回所读取的一个字节。如果读到文件末尾或者读取出错时返回EOF。

程序例

编辑
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#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;
}

Linux C

编辑

相关函数

open,fread,fscanf,getc

表头文件

include<stdio.h>

定义函数

int fgetc(FILE * stream);

函数说明

fgetc()从参数stream所指的文件中读取一个字符,并把它作为一个字符返回。若读到文件尾或出现错误时,它就返回EOF,你必须通过ferror或feof来区分这两种情况。

返回值

fgetc()会返回读取到的字符,若返回EOF则表示到了文件尾,或出现了错误。

范例

1
2
3
4
5
6
7
8
9
10
#include<stdio.h>
void main()
{
    FILE *fp;
    int c;
    fp=fopen("exist","r");
    while((c=fgetc(fp))!=EOF)
        printf("%c",c);
    fclose(fp);
}
0 0
原创粉丝点击