库函数调用IO接口

基础IO

  输入输出,程序(内存)和外部设备(键盘,显示器,磁盘等)交互的操作

标准库IO接口

打开文件

FILE * fopen ( const char * filename, const char * mode )

  • FILE* 文件流指针 成功返回文件指针 失败返回NULL

  • filename 文件

  • mode 打开方式
    打开方式介绍

  • r 只读 若文件不存在报错

  • r+ 读写 若文件不存在报错

  • w 只写 若文件不存在 创建 若文件存在 清空文件内容

  • w+ 读写 若文件不存在 创建 若文件存在 清空文件内容

  • a 若文件不存在 创建 如果文件存在,那么将写入的数据追加到文件的末尾(文件原有的内容保留)。

  • a+ 以“追加/更新”方式打开文件,相当于a和r+叠加的效果。既可以读取也可以写入,也就是随意更新文件。如果文件不存在,那么创建一个新文件;如果文件存在,那么将写入的数据追加到文件的末尾(文件原有的内容保留)
    注意:每次在文件读写时,文件内部的指针都会移动

关闭文件

int fclose ( FILE * stream )
If the stream is successfully closed, a zero value is returned.
On failure, EOF is returned.
返回值为0时正常关闭 失败返回EOF(通常为-1)

读取一个字符

int fgetc ( FILE * stream )
fgetc() 读取成功时返回读取到的字符,读取到文件末尾或读取失败时返回EOF
注意:读取到文件末尾和读取失败时都返回EOF
可以通过feof() 函数来判断文件内指针是否指向文尾
int feof ( FILE * stream ),如果指向文尾返回非0,否则返回0。文件内部指针在默认每次打开指向第一个字节。

也可以通过ferror() 函数用来判断文件操作是否出错,出错返回非0,否则返回0
int ferror ( FILE * stream )

写入一个字符

int fputc ( int character, FILE * stream )

On success, the character written is returned.
If a writing error occurs, EOF is returned and the error indicator (ferror) is set.
可以通过以下方式输入

    while ( (ch=getchar()) != '\n' ){
        fputc(ch,fp);
    }

读字符串函数 fgets

char * fgets ( char * str, int num, FILE * stream );

Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or the end-of-file is reached, whichever happens first.

A newline character makes fgets stop reading, but it is considered a valid character by the function and included in the string copied to str.
换行符也会被读取到

A terminating null character is automatically appended after the characters copied to str.

Notice that fgets is quite different from gets: not only fgets accepts a stream argument, but also allows to specify the maximum size of str and includes in the string any ending newline character.

返回值:
On success, the function returns str.
If the end-of-file is encountered while attempting to read a character, the eof indicator is set (feof). If this happens before any characters could be read, the pointer returned is a null pointer (and the contents of str remain unchanged).
If a read error occurs, the error indicator (ferror) is set and a null pointer is also returned (but the contents pointed by str may have changed).
成功返回str 失败返回NULL


    while(fgets(str, N, fp) != NULL){
        printf("%s", str);
    }

写字符串函数 fputs

int fputs ( const char * str, FILE * stream );
Write string to stream
Writes the C string pointed by str to the stream.

The function begins copying from the address specified (str) until it reaches the terminating null character (’\0’). This terminating null-character is not copied to the stream.不会追加\0

Notice that fputs not only differs from puts in that the destination stream can be specified, but also fputs does not write additional characters, while puts appends a newline character at the end automatically.

返回值:
On success, a non-negative value is returned. 成功返回非负
On error, the function returns EOF and sets the error indicator (ferror).错误返回EOF

fread和fwrite

以数据块的形式读写文件
size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );
size_t fwrite ( void * ptr, size_t size, size_t count, FILE * stream );

  • ptr 内存区块指针它可以是数组、变量、结构体等。fread() 中的 ptr 用来存放读取到的数据,fwrite() 中的 ptr 用来存放要写入的数据。
  • size 每个数据块字节数
  • count 要读取的数据块的块数
  • stream 文件指针
  • 理论上每次度数count*size个字节数据(正常情况下)

返回值:返回成功读取/写入的元素总数。

如果此数字与count参数不同,则在读取时发生读取错误或到达文件结尾。在这两种情况下,都设置了适当的指示器,可分别用feror和feof进行检查。

如果size或count为零,则函数返回零,流状态和ptr指向的内容保持不变。

size_t是无符号整数类型。

rewind

void rewind ( FILE * stream );
Set position of stream to the beginning
在打开文件后,系统会在文件内放置一个指针指向文件的第一个字节,读写都会导致指针的移动,通过rewind函数可以是这个指针从新指向文件开始地方,具有相同且更加强大功能的函数是fseek

fseek

int fseek ( FILE * stream, long int offset, int origin );
Reposition stream position indicator /重新定位文件的位置指示器)

  • offset 偏移量 从原点位置偏移的字节数(二进制文件) 0或者ftell返回值(文本文件)整数向右偏移 负数向左偏移
  • origin 起始位置 可以设定为以下三个参数 定义的是int型 分别为0 1 2
    SEEK_SET 文件开始位置 0
    SEEK_CUR 当前指示器位置 1
    SEEK_END 文件末尾 2
    返回值:成功返回0
    If successful, the function returns zero.
    Otherwise, it returns non-zero value.
    If a read or write error occurs, the error indicator (ferror) is set.
发布了89 篇原创文章 · 获赞 11 · 访问量 5289

猜你喜欢

转载自blog.csdn.net/weixin_44997886/article/details/104319364