嵌入式学习——文件IO(文字描述符)——day21

(1)标准IO——有缓存

            1.打开文件    fopen
            2.读写文件
                        fgetc/fputc
                        fgets/fputs 
                        fprintf/fscanf 
            3.关闭文件    fclose

1. 缓存(标准IO有缓存,文件IO没有缓存)——从流中读写数据时标准IO没有缓存

1.1 全缓存        4096字节(4k)

        刷新条件:
            1.缓存区满刷新
            2.fflush刷新
            3.fclose刷新

1.2 行缓存        1024字节(1k)stdin、stdout

        刷新条件:
            1.缓存区满刷新
            2.fflush刷新
            3.fclose刷新
            4.遇到\n刷新

1.3 不缓存        0k        stderr

        立即刷新不缓存    

(2)  二进制文件读写

            fwrite/fread

(3) 流的定位函数接口

1. fseek

1.1 定义

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

1.2 功能

        定位流的偏移量

1.3 参数

        stream:文件流指针
        offset:偏移量
            > 0 向后偏移
            < 0 向前偏移 
        whence: 偏移的基准   
            SEEK_SET        文件开头
            SEEK_CUR    文件当前位置
            SEEK_END    文件末尾

1.4   返回值

        成功返回0 
        失败返回-1 

1.5  示例代码

#include <stdio.h>

int main(void)
{
    char buf[512]={0};
    FILE* fp = fopen("1.c", "r");

    if(NULL == fp)
    {
        printf("fopen error!\n");
        return -1;
    }

    fseek(fp, 9, SEEK_SET);

    fgets(buf, sizeof(buf), fp);
    printf("%s", buf);

    fclose(fp);

    return 0;
}

2. rewind

2.1 定义

       void rewind(FILE *stream); 

2.2 功能

        将文件流指针偏移量定义为开头——等价于 fseek(stream, 0, SEEK_SET);

2.3 参数

        stream:文件流指针

2.4  示例代码

#include <stdio.h>

int main(void)
{
    char buf[512]={0};
    FILE* fp = fopen("1.c", "r");

    if(NULL == fp)
    {
        printf("fopen error!\n");
        return -1;
    }

    fseek(fp, 9, SEEK_SET);
    rewind(fp);

    fclose(fp);

    return 0;
}

3. ftell

3.1 定义

       long ftell(FILE *stream); 

3.2 功能

        获得流的偏移量

3.3 参数

        stream:文件流指针

3.4  示例代码

#include <stdio.h>

int main(void)
{
    char buf[512]={0};
    long num = 0;
    FILE* fp = fopen("1.c", "r");

    if(NULL == fp)
    {
        printf("fopen error!\n");
        return -1;
    }

    fseek(fp, 0, SEEK_END);
    num = ftell(fp);
    rewind(fp);

    printf("num = %ld\n", num);

    fclose(fp);

    return 0;
}

(4) 文件IO步骤

        1. open/close

        2. read/write

        3. lseek

(5)文件IO函数接口

1 open

        1. 定义

            int open(const char *pathname, int flags);
            int open(const char *pathname, int flags, mode_t mode);   

        2. 功能

                打开文件并获得操作文件的文件描述符

        3. 参数

                pathname:要打开的文件路径对应字符串的首地址

                flags:
                    O_RDONLY        只读
                    O_WRONLY        只写
                    O_RDWR            读写
            
                    O_CREAT            文件不存在创建(需要open传入第三个参数)
                    O_TRUNC            文件存在截断成0(清0)
                    O_APPEND          追加打开
                    O_ASYNC             异步IO
                    O_NONBLOCK     非阻塞IO
                    O_EXCL                检测文件是否存在 

        4. 返回值

                成功返回新文件描述符
                失败返回-1 

        5. 示例程序

        注意使用open时要加头文件

        #include <sys/types.h>
        #include <sys/stat.h>
        #include <fcntl.h>

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

int main(void)
{
    int fd = 0;

    fd = open("1.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
    if (-1 == fd)
    {
        printf("open failed!\n");
        return -1;
    }

    return 0;
}

2 close

        1. 定义

                int close(int fd); 

        2. 功能

                关闭文件描述符

        3. 参数

                fd  文件描述符

        4. 返回值

                成功返回0 
                失败返回-1 

        5. 示例程序

        注意使用close时要加头文件

        #include <unistd.h>

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

int main(void)
{
    int fd = 0;

    fd = open("1.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
    if (-1 == fd)
    {
        printf("open failed!\n");
        return -1;
    }

    close(fd);

    return 0;
}

3 write

        1. 定义

                ssize_t write(int fd, const void *buf, size_t count); 

        2. 功能

                向文件描述符对应文件中写入buf开始的count个字节数据

        3. 参数

                fd:文件描述符
                buf:存放数据空间的首地址
                count:写入字节的个数

        4. 返回值

                成功返回实际写入的字节数
                失败返回-1  

        5. 示例程序

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

int main(void)
{
    int fd = 0;
    ssize_t size = 0;
    char buf[32] = "supercarrydoinb";

    fd = open("2.c", O_WRONLY | O_CREAT | O_TRUNC, 0666);
    if (-1 == fd)
    {
        printf("open error!\n");
        return -1;
    }

    size = write(fd, buf, strlen(buf));
    printf("size = %ld\n", size);

    close(fd);

    return 0;
}

4 read

        1. 定义

                ssize_t read(int fd, void *buf, size_t count); 

        2. 功能

                从文件描述符中读取count个字节,放入buf指向的空间中

        3. 参数

                fd:文件描述符 
                buf:存放数据空间首地址 
                count:最多读取数据字节数

        4. 返回值

                成功返回实际读到字节数
                失败返回-1 
                读到文件末尾返回0 

        5. 示例程序

                1.简单程序 

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

int main(void)
{
    int fd = 0;
    ssize_t num = 0;
    char buf[1024] = {0};

    fd = open("2.c", O_RDONLY);
    if (-1 == fd)
    {
        printf("open error!\n");
        return -1;
    }

    num = read(fd, buf, sizeof(buf));
    printf("buf = %s\n", buf);
    printf("num = %ld\n", num);

    close(fd);

    return 0;
}

                2. 使用write和read完成图片的复制

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

int main(void)
{
    int src = 0;
    int dst = 0;

    src = open("1.png", O_RDONLY);
    dst = open("3.png", O_WRONLY | O_CREAT | O_TRUNC, 0666);
    if (-1 == src || -1 == dst)
    {
        printf("open error!\n");
        return -1;
    }

    while (1)
    {
        char buf[1024] = {0};
        ssize_t size = 0;

        size = read(src, buf, sizeof(buf));
        if (0 == size)
        {
            break;
        }
        write(dst, buf, sizeof(buf));
    }

    close(src);
    close(dst);

    return 0;
}

(6) 文件IO偏移量定位函数接口

        lseek

        1. 定义

                off_t lseek(int fd, off_t offset, int whence);

        2. 功能

                重定位文件描述符的偏移量

        3. 参数

            fd:文件描述符
            offset:偏移量
                >0 向后偏移
                <0 向前偏移
            whence:
                SEEK_SET    文件开头
                SEEK_CUR    文件当前位置
                SEEK_END    文件末尾

        4. 返回值

            成功返回偏移量
            失败返回-1 

        5. 示例程序

                

              

  • 19
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值