C语言读入文件的函数列举

read, fopen, fread, fgetc,getc() 函数

getc()函数--读文件函数(由文件中读取一个字符)

头文件:#include <stdio.h>

定义函数:int getc(FILE * stream);

函数说明:getc()用来从参数stream 所指的文件中读取一个字符. 若读到文件尾而无数据时便返回EOF. 虽然getc()与fgetc()作用相同, 但getc()为宏定义, 非真正的函数调用.

返回值:getc()会返回读取到的字符, 若返回EOF 则表示到了文件尾.

范例参考fgetc().

fgets()—读取文件字符串:

相关函数:open, fread, fscanf, getc

头文件:include<stdio.h>

定义函数:har * fgets(char * s, int size, FILE * stream);

函数说明:fgets()用来从参数stream 所指的文件内读入字符并存到参数s 所指的内存空间, 直到出现换行字符、读到文件尾或是已读了size-1 个字符为止, 最后会加上NULL 作为字符串结束.

返回值:gets()若成功则返回s 指针, 返回NULL 则表示有错误发生.

范例
#include <stdio.h>
main()
{
    char s[80];
    fputs(fgets(s, 80, stdin), stdout);
}

执行
this is a test //输入
this is a test //输出

getchar()—字符输入函数(由标准输入设备内读进一字符)

相关函数:fopen, fread, fscanf, getc

头文件:#include <stdio.h>

定义函数:int getchar(void);

函数说明:getchar()用来从标准输入设备中读取一个字符. 然后将该字符从unsigned char 转换成int 后返回.

返回值:getchar()会返回读取到的字符, 若返回EOF 则表示有错误发生.

附加说明:getchar()非真正函数, 而是getc(stdin)宏定义.

范例
#include <stdio.h>
main()
{
    FILE * fp;
    int c, i;
    for(i = 0; i < 5; i++)
    {
        c = getchar();
        putchar(c);
    }
}

执行
1234 //输入
1234 //输出

 
原文地址:https://www.cnblogs.com/zhoususheng/p/2965345.html