Linux C语言函数API--标准I/O函数

二:标准I/O函数

1.fopen函数

【原型】    FILE *fopen(const char *path,const char *mode);
【功能】    获取指定文件的文件指针
【头文件】 #include <stdio.h>

【参数path】 即将要打开的文件
【参数mode】    "r" :以只读方式打开文件,要求文件必须存在。
                "r+":以读写方式打开文件,要求文件必须存在。
                "w" :以只写方式打开文件,文件如果不存在将会创建新文件,如果存在将会将其内容清空。
                "w+":以读写方式打开文件,文件如果不存在将会创建新文件,如果存在将会将其内容清空。
                "a" :以只写方式打开文件,文件如果不存在将会创建新文件,且文件位置偏移量被自动定位到文件末尾(即追加方式写数据).
【返回值】 成功: 文件指针  失败 :NULL


代码演示:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>


int main(int argc,const **argv)
{   FILE *F = NULL;
    int n;
    F = fopen("./2.txt","r+");
    printf("2.txt的文件指针为:%p\n",F);
    char buf[8]={'1','2','3','4','5','6','7','8'};
    fwrite(buf,4,2,F);      
    //将字符数组buf里面的数据按照4个字节,每次写入2块进去文件F里面
    char *p="abcdefghijklmn";
    fwrite(p,1,strlen(p),F);
    
    fclose(F);
    return 0;
}

2.fclose函数


【原型】    int fclose(FILE *fp);

【功能】    关闭指定的文件并释放其资源

【头文件】 #include<stdio.h>

【参数fp】  即将要关闭的文件
【返回值】 成功:0 失败 EOF

=========================================================================================

3.fgetc函数

【原型】    int fgetc(FILE *stream);
【功能】    获取指定文件的一个字符
【头文件】 #include <stdio.h>
【参数stream】   文件指针

【返回值】 成功 读取到的字符   失败: EOF
【备注】    当返回EOF时,文件stream可能已经达末尾,或者遇到错误。



代码演示:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>


int main(int argc,const **argv)
{   FILE *F = NULL;
    int n;
    F = fopen("./2.txt","r+");
    while(n != EOF)
    {
    n = fgetc(F);           //从文件指针一个一个字节读取,直到返回值为EOF的时候退出
    printf("%c",n);
    if(n=='n')
    printf("\n");
    }

    fclose(F);
    return 0;
}


------------------------------------------------------------------------------

4.getc函数

【原型】  int getc(FILE *stream);
【功能】    获取指定文件的一个字符
【头文件】 #include <stdio.h>
【参数stream】   文件指针

【返回值】 成功 读取到的字符   失败: EOF
【备注】    当返回EOF时,文件stream可能已经达末尾,或者遇到错误。

代码演示:
F = fopen("./2.txt","r+");
    j = getc(F);                //从文件中一次读取一个字节
    printf("\t\t%c\n",j);
    
    fclose(F);

5.getchar函数

【原型】  int getchar(void);
【功能】    获取指定文件的一个字符
【头文件】 #include <stdio.h>
【返回值】 成功 读取到的字符   失败: EOF
【备注】    当返回EOF时,文件stream可能已经达末尾,或者遇到错误。
代码演示:

F = fopen("./2.txt","r+");
    k = getchar();                  //每次从键盘上读取一个字符
    printf("%c\n",k);
    fclose(F);
    

6.fputc函数

【原型】    int fputc(int c,FILE *stream);
【功能】    将一个字符写入到一个指定的文件。
【头文件】 #include <stdio.h>
【参数 C】 要写入的字符
【参数stream】 写入的文件指针
【返回值】 成功:写入到的字符 失败 EOF
代码演示:

F = fopen("./2.txt","r+");
    i = fputc('G',F);                //每次将一个字符输入到文件中
    printf("%c\n",i);               //此处打印的是G。
    //打开文件2.txt,发现G写在首位
    fclose(F);

7.putc函数


【原型】    int putc(int c,FILE *stream);
【功能】    将一个字符写入到一个指定的文件。
【头文件】 #include <stdio.h>
【参数 C】 要写入的字符
【参数stream】 写入的文件指针
【返回值】 成功:写入到的字符 失败 EOF
代码演示:
    F = fopen("./2.txt","r+");
    
    l = putc('h',F);
    printf("%d\n",l);
    fclose(F);

8.putchar函数


【原型】    int putchar(int c);
【功能】    将一个字符写入到一个指定的文件。
【头文件】 #include <stdio.h>
【参数 C】 要写入的字符
【参数stream】 写入的文件指针
【返回值】 成功:写入到的字符 失败 EOF

代码演示:

        putchar('\n');
        putchar('r');
        putchar('b');
        putchar('\n');fgec(),getc(),getchar()注意点】
            1.fgec(),getc()getchar()返回值是int 而不是char,原因是因为他们在出错或者读到文件末尾的时候需要
            返回一个值为 -1EOF 标记,而char 型数据有可能因为系统的差异而无法表示负整数。
            2.fgec(),getc()getchar()返回EOF时候,有可能发生了错误,也有可能是读到了文件末尾,这是要用以下两个函数
            接口来进一步加以判断:

9.feof函数

【原型】    int feof(FILE *stream);
【功能】    判断一个文件是否达到文件末尾
【头文件】 #include <sys/ioctl.h>
【参数】    stream 进行判断的文件指针
【返回值】 如果文件已达到末尾则返回真(0),否则返回假 0。
代码演示:
    FILE *F = NULL;
    int n;
    F = fopen("./2.txt","r+");
    n = feof(F);                //这里返回值为0,则是假 
    printf("%d\n",n);
    
    return 0;

10.ferror函数

【原型】    int ferror(FILE *stream);
【功能】    判断一个文件是否遇到了某种错误。
【头文件】 #include <sys/ioctl.h>
【参数】    stream 进行判断的文件指针
【返回值】 如果文件遇到错误则返回真,否则返回假。

代码演示:

    FILE *F = NULL;
    int n;
    F = fopen("./2.txt","r+");
    n = ferror(F);              //这里返回值为0,则为假,说明文件没有错误 
    printf("%d\n",n);
    
    return 0;

11.fgets函数

【原型】    char *fgets(char *s ,int size, FILE *stream);
【功能】    从指定文件读取最多一行数据
【头文件】 #include <sys/ioctl.h>

【参数】    s:自定义缓冲区指针
            size :自定义缓冲区大小
            stream :即将被读取数据的文件指针。

【返回值】 成功: 自定义缓冲区指针s 失败 :NULL

【备注】    1.get()缺省从文件stdin读入数据
            2.当返回NULL时,文件stream 可能已达末尾或者遇到错误。
  

代码演示:
    FILE *F = NULL;
    char buf[200];
    int i;
    char *p = NULL;
    int n;
    F = fopen("./2.txt","r+");
    p = fgets(buf,200,F);           //从文件F中读取一行数据,存放到内存大小为200的数组里面
    printf("%p\n",p);
    for(i=0; i<200;i++)
    {
        printf("%c\n",buf[i]);      //将读取的字符打印出来。
    }

12.gets函数

【原型】    char *gets(char *s);
【功能】    从指定文件读取最多一行数据
【头文件】 #include <sys/ioctl.h>

【参数】    s:自定义缓冲区指针

【返回值】 成功: 自定义缓冲区指针s 失败 :NULL

【备注】    1.get()缺省从文件stdin读入数据
            2.当返回NULL时,文件stream 可能已达末尾或者遇到错误。
   

代码演示:    

    char buf[10];
    int i;
    char *p = NULL;
    int n;
    p = gets(buf);          //从键盘中输入一行数据,存放到内存大小为200的数组里面
    for(i = 0;i<10;i++)
    {
        printf("%c",buf[i]);        //将键盘输入的数据打印出来
    }
   

13.fputs函数

【原型】    int fputs(const char *s,FILE *stream);
【功能】    将数据写入到指定的文件

【头文件】 #include <sys/ioctl.h>

【参数】    s:自定义缓冲区指针
            stream :即将被写入数据的文件指针。
【返回值】     成功 :非负整数  失败  EOF
【备注】    puts()缺省将数据写入文件stdout

代码演示:
FILE *F = fopen("./2.txt","r+");
    char buf[10] = {'1','2','3','4','5','6','7','8','9','10'};
    int i;
    i  = fputs(buf,F);      //将buf数组里面的数据写入到文件为F中。
    printf("%d\n",i);       //此时返回了一个非0正数,成功
    return 0;
   
-----------------------------------------------------------

【注意点】     
            1.fgets()fgetc()一样,当其返回NULL时候并不能确定究竟是达到文件末还是碰到错误,
                需要用feof()/ferror()来进一步判断。
            2.fgets()每次读取到之多不超过size个字节的一行,所谓"一行",即数据至多包含多个换行符'\n'.
            3.get()是一个已经过时的接口,因为他没有指定自定义缓冲区s的大小,这样很容易造成缓冲区溢出,导致程序段访问错误。
            4.fgets()fputs()gets()puts()一般成对使用,鉴于gets()的不安全性,一般建议使用前者。

            
=====================================================================================           
            

14.fread函数

【原型】    size_t fread(void *ptr,size_t size, size_t nmemb,FILE *stream
【功能】    从指定文件读取若干个数据块
【头文件】 #include <sys/ioctl.h>
【参数】    ptr : 自定义缓冲区指针
            size :数据块大小
            nmemb :数据块个数
            stream :即将被读取数据的文件指针。
【返回值】     成功 :读取的数据块个数,等于sinmembze   失败: 读取的数据块个数,小于nmemb 或等于0
【备注】    当返回小于nmemb时,文件stream可能已达到末尾,或者遇到错误。

代码演示:

    FILE *F = fopen("./2.txt","r+");
    int i,n;            //i为返回的块数
    char buf[10];
    i = fread(buf,2,5,F);
    printf("%d\n",i);
    for(n=0;n<10;n++)
    {
        printf("%c",buf[n]);
    }
--------------------------------------------------------------------------------------

15.fwrite函数

【原型】    size_t fwrite(const void *ptr,size_t size, size_t nmemb,FILE *stream);
【功能】    将若干块数据写入指定的文件
【头文件】 #include <sys/ioctl.h>
【参数】    ptr:自定义缓冲区大小
            size :数据块大小
            nmemb :数据块个数
            stream :即将被写入数据的文件指针
【返回值】 成功 :写入的数据块个数,等于sinmembze,失败,写入的数据块个数小于nmemb或等于0

【备注】    1.如果fread()返回值小于nmemb时候,可能已达到末尾,或者遇到错误,需要借助于feof()/ferror()来加以进一步判断。
            2.当发生上述第1种情况时,其返回值并不能真正反映其读取或者写入的数据块数,而只是一个所谓的"截短值"
 代码演示:          


    FILE *F = fopen("./2.txt","r+");
    int i,n;            //i为返回的块数
    char buf[10] = {'1','2','3','4','5','6','7','8','9'};
    i = fwrite(buf,2,5,F);          //将buf数组里面的数据写入到文件F中。
    printf("%d\n",i);
    for(n=0;n<10;n++)
    {
        printf("%c",buf[n]);
    }

16.fseek函数

【原型】    int fseek(FILE *stream,long offset,int whence);
【功能】    设置指定文件的当前位置偏移量
【参数】    stream :需要设置偏移量的文件指针
            offset :新位置偏移量相对基准点的偏移
            whence :基准点 1.SEEK_SET :文件开头处
                            2.SEEK_CUT :当前位置
                            3.SEEK_END :文件末尾处
                           
【返回值】 成功 0 失败 -1

代码演示:
FILE *F = fopen("./2.txt","r+");
    int i;
    i = fseek(F,5,SEEK_SET);            //将文件F从首位定位,定位5,此时返回的为0 ,说明成功了。
    printf("%d\n",i);


17.ftell函数

【原型】    long ftell(FILE *stream);
【功能】    获取指定文件的当前位置偏移量。
【头文件】 #include <sys/ioctl.h>
【参数】    stream :需要返回当前文件位置偏移量的文件指针
【返回值】  成功:当前位置偏移量 失败 -1


代码演示:

FILE *F = fopen("./2.txt","r+");
    int i;
    fseek(F,5,SEEK_SET);            //将文件F从首位定位,定位5,此时返回的为0 ,说明成功了。
    
     i = ftell(F);  //从文件F获取指定偏移量数据,
    printf("%d\n",i);               //此处打印为5,说明指定偏移量为5


-----------------------------------------------------------------------------------------+

18.rewind函数

【原型】    void rewind(FILE *stream);
【功能】    将指定文件的当前位置偏移量设置到文件开头处。
【头文件】 #include<sys/ioctl.h>
【参数】    stream:需要设置位置偏移量的文件指针。
【返回值】 无
【备注】 该函数的功能是将文件stream的位置偏移量置位到文件开头处。


【注意】    
        1. fseek()的用法基本上跟系统IO的lseek()是一致的。
        2.rewind(fp)相等于fseek(fp,0L,SEEK_SE);
  

代码演示:
FILE *F = fopen("./2.txt","r+");
    int i;
    fseek(F,5,SEEK_SET);            //将文件F从首位定位,定位5,此时返回的为0 ,说明成功了。
    
     i = ftell(F);  //从文件F获取指定偏移量数据,
    printf("%d\n",i);               //此处打印为5,说明指定偏移量为5
    
    rewind(F);                      //重置文件指针
     i = ftell(F); 
    printf("%d\n",i);               //此处打印为0,说明重置指针成功

        
==============================================================================

19.fprint函数

【原型】    int fprintf(FILE *restrict stream, const char *restrict format,...);
【功能】    将格式化数据写入指定的文件或者内存
【头文件】 #include<stdio.h>

【参数】    stream :写入数据的文件指针 
            format 格式控制串
【返回值】 成功 :成功写入的字节数      失败:-1

代码演示:

-------------------------------------------------------------------------------
    FILE *F = fopen("./2.txt","r+");
    char *p = "ABCDEFG";
    char s[] = "abcdefg";
    int i = 5563453;
    fprintf(F,"%d\n,%s\n,%s\n",i,s,p);          //将数据写入到文件之中。
    printf("%d\n",num);                         //成功写入到的字节数


------------------------------------------------------------------------------

20.printf函数

【原型】    int printf(const char *restrict format,...);
【功能】    将格式化数据写入指定的文件或者内存
【头文件】 #include<stdio.h>

【参数】    format 格式控制串
            
【返回值】 成功 :成功写入的字节数      失败:-1

21.snprintf函数

【原型】    int snprintf(char *restrict s,size_t n,const char *restrict format,...);
【功能】    将格式化数据写入指定的文件或者内存
【头文件】 #include<stdio.h>

【参数】    
            format 格式控制串
            s :写入数据的自定义缓冲区
            n : 自定义缓冲区的大小
【返回值】 成功 :成功写入的字节数      失败:-1

代码演示:

    char *p = "ABCDEFG";
    char s[] = "";
    int i = 5563453;
    int num;
    num = snprintf(s,100,"%s\n",p);         //将数据写入到文件之中。
    printf("%d\n",num);                         //成功写入到的字节数
    printf("%s\n",s);                       //写入数据到s成功打印

22.sprintf函数

【原型】    int sprintf(char *restrict s,const char *restrict format,...);
【功能】    将格式化数据写入指定的文件或者内存
【头文件】 #include<stdio.h>

【参数】    
            format 格式控制串
            s :写入数据的自定义缓冲区
            
【返回值】 成功 :成功写入的字节数      失败:-1

代码演示:

char *p = "ABCDEFG";
    char s[] = "";
    int num;
    num = sprintf(s,"%s\n",p);          //将数据写入到文件之中。
    printf("%d\n",num);                         //成功写入到的字节数
    printf("%s\n",s);                       //写入数据到s成功打印

23.fscanf函数

【原型】    int fscanf(FILE *restrict stream,const char *restrict format,...);

【功能】    从指定的文件或者内存中读取格式化数据
【头文件】 #include <stdio.h>
【参数】    stream :读出数据的文件指针
            format :格式化控制指针
            
【返回值】 成功 :正确匹配且赋值的数据个数  失败 :EOF 


代码演示:


FILE *F = fopen("./2.txt","r+");
    char s[10];                         //为何这里的大小小于等于3就全部打印了?
    char c[10];
    int num;
    rewind(F);   //重置文件指针
    num = fscanf(F,"%s %s",s,c);
    //num = fscanf(F,"%s,%s",s,c);      注意:这里%s,%s中间的,是错误的。
    printf("%s\n",s);
    printf("%s\n",c);
    printf("%d\n",num);

    fclose(F);

————————————————————————————————————————————————

24.scanf函数

【原型】    int scanf(const char *restrict format,...);

【功能】    从指定的文件或者内存中读取格式化数据
【头文件】 #include <stdio.h>
【参数】    
            format :读出数据的文件指针
            
【返回值】 成功 :正确匹配且赋值的数据个数  失败 :EOF 

25.sscanf函数

【原型】    int sscanf(const char *restrict s,const char *restrict format,...);

【功能】    从指定的文件或者内存中读取格式化数据
【头文件】 #include <stdio.h>
【参数】    
            format :格式控制串
            s:读出数据的自定义缓冲区
【返回值】 成功 :正确匹配且赋值的数据个数  失败 :EOF 
代码演示:

	===========================================
		char str[100]="234"; 
        sscanf(str,"%s",str);
        printf("%s\n",str);		//打印出的数据为234
	==========================================
  char str[100];  
    //用法一:取指定长度的字符串  
    sscanf("12345","%4s",str);  
    printf("用法一\nstr = %s\n",str);  
  
    //用法二:格式化时间  
    int year,month,day,hour,minute,second;  
    sscanf("2013/02/13 14:55:34","%d/%d/%d %d:%d:%d",&year,&month,&day,&hour,&minute,&second);  
    printf("用法二\ntime = %d-%d-%d %d:%d:%d\n",year,month,day,hour,minute,second);  
  
    //用法三:读入字符串  
    sscanf("12345","%s",str);  
    printf("用法三\nstr = %s\n",str);  
  
    //用法四:%*d 和 %*s 加了星号 (*) 表示跳过此数据不读入. (也就是不把此数据读入参数中)  
    sscanf("12345acc","%*d%s",str);  
    printf("用法四\nstr = %s\n",str);  
  
    //用法五:取到指定字符为止的字符串。如在下例中,取遇到'+'为止字符串。  
    sscanf("12345+acc","%[^+]",str);  
    printf("用法五\nstr = %s\n",str);  
  
    //用法六:取到指定字符集为止的字符串。如在下例中,取遇到小写字母为止的字符串。  
    sscanf("12345+acc121","%[^a-z]",str);  
    printf("用法六\nstr = %s\n",str);  
    return 0;

————————————————————————————————————————————————————————————

============================================================================

26.stat函数

【原型】    int stat(const char *path,struct stat *buf);
【功能】    获取文件的元数据(类型、权限、大小等等.....)
【头文件】 #include <sys/types.h>
            #include <sys/stat.h>
            #include <unistd.h>
【参数】    path:文件路径
            
            buf:属性结构体
【返回值】 成功 0 失败NULL

27.fstat函数

【原型】    int fstat(int fd,struct stat *buf);
【功能】    获取文件的元数据(类型、权限、大小等等.....)
【头文件】 #include <sys/types.h>
            #include <sys/stat.h>
            #include <unistd.h>
【参数】    
            fd:文件描述符 
            buf:属性结构体
【返回值】 成功 0 失败NULL

28.lstat函数

【原型】    int lstat(const char *path,struct stat *buf);
【功能】    获取文件的元数据(类型、权限、大小等等.....)
【头文件】 #include <sys/types.h>
            #include <sys/stat.h>
            #include <unistd.h>
【参数】    path:文件路径
            
            buf:属性结构体
【返回值】 成功 0 失败NULL



【属性结构体如下】

struct stat
{
    dev_t st_dev;       //普通文件所在存储器的设备号
    mode_t st_mode;     //文件类型,文件权限
    ino_t st_ino;       //文件索引号
    nlink_t st_mlink;   //引用计数
    uid_t st_uid;       //文件所有者的UID
    gid_t st_gid;       //文件所属组的GID
    dev_t st_rdev;      //特殊文件的设备号
    off_t st_size;      //文件大小
    blkcnt_t st_blocks; //文件所占数据块数目
    time_t  st_atime;   //最近访问时间
    time_t st_mtime;    //最近修改时间
    time_t st_ctime;    //最近属性更改时间
    blksize_t st_blksize;   //写数据块建议值
};


========================================================

29.major函数

【原型】    int major(dev_t dev);
            
【功能】    返回设备号
【头文件】 #include <sys/types.h>
【参数】    dev:文件的设备号属性,来自stat结构体中的st_dev或者st_rdev
【返回值】 成功:返回主设备号  失败 无

30.minor函数

【原型】    
            int minor(dev_t dev);
【功能】    返回设备号
【头文件】 #include <sys/types.h>
【参数】    dev:文件的设备号属性,来自stat结构体中的st_dev或者st_rdev
【返回值】 成功:返回次设备号  失败 无


=================================================================
发布了9 篇原创文章 · 获赞 10 · 访问量 238

猜你喜欢

转载自blog.csdn.net/my___sunshine/article/details/103999503