zoukankan      html  css  js  c++  java
  • 文件IO学习

    标准IO库

    流与文件对象

    对应标准IO库,对文件的读写操作都是围绕着流(stream)进行的

    流(文本流或者二进制流)主要是指一种逻辑上的概念,它提供或存储数据。产生数据的叫输入流,消耗数据的叫输出流。流的概念使我们不再关注具体细节和设备本身,是一个抽象的概念

    缓冲

    标准IO库提供缓冲的目的是尽可能的减少read和write的次数。1、全缓冲  2、行缓冲(当流涉及到终端时,通常是行缓冲) 3、无缓冲,不经过缓冲区直接写入

    FILE* fopen(const char*restrict pathname , const char* restrict type);

    成功则返回文件指针,出错则返回NULL

    type 对应参数 r , w , a , r+ , w+ , a+分别对应只读,只写,追加,后三种在前三种的基础上均是以读写的方式打开。加b均可对应相应的二进制文件

    int fclose(FILE* fp);

    关闭一个打开的流,出错则返回EOF。

    打开流之后,三种不同类型IO可选择

    1.一个字符  2.一行字符  3.直接IO(fread fwrite)

    1. int getc(FILE* fp)

    2.int fgetc(FILE* fp)

    3.int getchar(void)

    每个流在FILE对象中维持了两个标志

    出错标志

    文件结束标志

    输出函数

    1.int putc(int c , FILE* fp)

    2.int fputc(int c,FILE* fp)

    3.int putchar(int c)

    每次一行IO

    输入

    char * fgets(char* restrict buf , int n , FILE* restrict fp);

    char* gets(char* buf)

    输出

    int fputs(const char* restrict str,FILE* restrict fp);

    int puts(const char* str);

    二进制IO

    size_t fread(void* restrict ptr, size_t size , size_t nobj, FILE* restrict fp);

    size_t fwrite(void* restrict ptr, size_t size , size_t nobj, FILE* restrict fp);

    定位流

    long ftell(FILE* fp) 

    若成功则返回当前文件位置指示,出错则返回-1

    int fseek(FILE* fp, long offset , int whence)

    如果执行成功,stream将指向以fromwhere(偏移起始位置:文件头0(SEEK_SET),当前位置1(SEEK_CUR),文件尾2(SEEK_END))为基准,偏移offset(指针偏移量)个字节的位置

    格式化输出

    int fprintf(FILE* restrict fp , const char*restrict format,..)

    若成功则返回输出字节数,若输出出错则返回负值

  • 相关阅读:
    python json模块(15)
    python random模块(14)
    python time模块(13)
    python sys模块(12)
    python zip函数(11)
    python递归函数(10)
    python 浅拷贝和深拷贝(9)
    python is 和 == 区别(8)
    python 可变数据类型和不可变数据类型(7)
    python局部变量和全局变量(6)
  • 原文地址:https://www.cnblogs.com/doulcl/p/10942342.html
Copyright © 2011-2022 走看看