1.read
用于读取一个已打开的文件的内容。
2.头文件
#include <unistd.h>
3.函数原型
ssize_t read(int fd, void *buf, size_t count);
4.参数
fd:表示要操作文件的文件描述符。
buf:表示读出数据存放的位置(缓冲区地址)。
count:表示读出的字节数。
5.返回值
若读取成功,则返回读到的字节数;若失败,返回-1;若已达到文件尾,则返回0。
6.示例:(读取前面写入的test文件)
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
        char buf[30];
        int fd = open("./test", O_RDWR);
        if (fd < 0) {
                printf("error: test open\n");
                return -1;
        }
        if (read(fd, buf, 30) < 0) {
                printf("error: test read\n");
                return -1;
        }
        printf("read fd:%s", buf);
       
        return 0;
}
7.编译运行并查看测试结果
read fd:HELLO WORLD!!!

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    Powered by Discuz! X3.5  © 2001-2013 Comsenz Inc.