Linux系统编程之--文件操作

前言:Linux系统之下皆文件

1、缓冲区文件操作 -- 普通文件(.TXT .MP3 .MP4...)

        缓冲区:文件的读写并不是直接操作文件的,而是操作缓冲区的(RAM)

        例如:写一个字符Q到某.txt文件中,并不是真正写到了.txt文件中,而是写到了系统的内存里面(RAM---内存条),等待Ctrl+S或者手动保存、程序结束、遇到特殊符号才会把之前写到内存里面的东西一次性写入文件里面。

        优点:提高CPU的利用率。

缓冲区文件API:

      1.1 fopen

       函数的功能:缓冲区操作打开一个普通文件

        函数头文件:<stdio.h>

        函数原型:

                FILE *fopen(const char *pathname, const char *mode);

函数的参数:

    pathname:

        打开文件的路径(可以是绝对路径也可以是相对路径)

    mode:

        r      Open text file for reading.  The stream is positioned at the beginning of
              the file.

       r+     Open for reading and writing.  The stream is positioned at the  beginning
              of the file.

       w      Truncate file to zero length or create text file for writing.  The stream
              is positioned at the beginning of the file.

       w+     Open for reading and writing.  The file is created if it does not  exist,
              otherwise  it is truncated.  The stream is positioned at the beginning of
              the file.

       a      Open for appending (writing at end of file).  The file is created  if  it
              does not exist.  The stream is positioned at the end of the file.

       a+     Open  for  reading  and  appending (writing at end of file).  The file is
              created if it does not exist.  The initial file position for  reading  is
              at the beginning of the file, but output is always appended to the end of
              the file.
The mode string can also include the letter 'b' either as a last character or as a character between the characters in any of the two-character strings described above.  This is strictly for compatibility with C89 and has no effect;  the  'b' is ignored on all POSIX conforming systems, including Linux.  (Other systems may treat text files and binary files differently, and adding the 'b' may be a  good idea  if  you do I/O to a binary file and expect that your program may be ported to non-UNIX environments.)
函数的返回值:FILE *

FILE * 返回的指针内部是由 malloc 开辟的堆空间,由于malloc 开辟必然有 free 释放, fclose 本质就是释放 fopen 开辟的malloc FILE * 指针, FILE * 指针是一个复杂的结构体但是该结构体不需要看  因为FILE 这个指针返回值是给后续函数fclose  fread  fwrite使用。

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

        1.2 fclose

函数的功能:关闭/释放一个已经打开的缓冲区文件指针

函数头文件:<stdio.h>

函数的原型:

    int fclose(FILE *stream);

函数的参数:

    stream:()

        代表你用 fopen打开文件的返回值

函数的返回值:

    潜规则:

        一般返回值为 int 类型的

        如果没有特殊意义

            返回值为 0 则代表成功

            返回值为 0 代表失败 (-1)

        EOF-->  -1

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

函数的功能:读缓冲区的一个文件

函数头文件:<stdio.h>

函数的原型:

    size_t fread(

            void *ptr,

            size_t size,

            size_t nmemb,

            FILE *stream

        );     

函数的参数:

    ptr:

        void * 在这里面叫做万能指针

                可以是任意类型!

                读写操作支持C语言的所有的类型的数据

        读取内容存放的缓冲区--空间

    size:

        size_t -- long unsigned int

        他就是 一次 读取的内容的字节数

       

    nmemb:

        读几次

   

    stream:

        你要读的文件流指针

函数的返回值

    返回成功读写的次数!

    如果一次都未成功、或者读取内容不足一次返回 0

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

函数的功能:写入内容到缓冲区的文件里面

函数头文件:<stdio.h>

函数的原型:

    size_t fwrite(

            const void *ptr,

            size_t size,

            size_t nmemb,

            FILE *stream

        );

函数的参数:

    ptr:

        const:常量--代表你传入的内容无法修改

        这个就是你要写入的内容是什么

    size:

        你写入一次的大小字节数

    nmemb:

        你写入几次!

    stream:

        写入到哪个文件里面

函数返回值:

    请参考 fread ..

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

函数的功能:偏移读写指针

函数头文件:<stdio.h>

函数的原型:

    int fseek(FILE *stream, long offset, int whence);

函数的参数:

    stream:

        你现在要对哪个文件的读写指针进行偏移

    offset:

        偏移的数量大小

        10:

            往当前参考位置指针右边偏移10个字节

        -10:

            往当前参考位置指针左边偏移10个字节

        0

            在当前参考位置 不偏移

    whence:

        SEEK_END: 末尾为参考位置

        SEEK_SET:开头为参考位置

        SEEK_CUR:当前原在位置为参考位置

        #define SEEK_SET    0   /* Seek from beginning of file.  */

        #define SEEK_CUR    1   /* Seek from current position.  */

        #define SEEK_END    2   /* Seek from end of file.  */

函数返回值: 

    符合我们之前讲的潜规则

        成功返回 0

        失败返回 -1

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

函数的功能:直接讲读写指针偏移到开头

函数头文件:<stdio>

函数的原型: 

    void rewind(FILE *stream);

函数的参数:

    stream:

        你要把哪个文件的读写指针偏移到开头

函数返回值:

   

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

函数的功能:检查文件是否达到末尾

函数头文件:<stdio.h>

函数的原型:

    long ftell(FILE *stream);

函数的参数:

    stream:

        检查哪个文件

函数的返回值:

    如果达到末尾返回 -1

    否则返回其他

2、非缓冲区文件操作 -- 系统内部文件(管道文件、套接字文件、块设备文件、 字符设备文件)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Andy.w

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值