为了账号安全,请及时绑定邮箱和手机立即绑定

UNIX-文件IO函数使用范例

标签:
Linux C
/*
文件I/O常用函数使用范例
*/

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>

void open_readMode(const char*);
void open_writeMode(const char*);
void open_lseek(const char *pathname);
void standard_io(const char *ptr_input);

int main()
{
    const char *pathname = "./file.rtf";
    const char *pathname2 = "./file2";
    //open_readMode(pathname);
    //open_writeMode(pathname);
    //open_lseek(pathname2);
    standard_io("hello, standard io, welcome to code world!\n");
    return 0;
}

void open_readMode(const char *pathname)
{
    int fd;
    char buf[1024] = {0};
    const char *buf2 = "is omp";

    /*只读方式打开,无法write*/
    /*int open(const char *pathname, int oflag, mode_t mode)*/
    fd = open(pathname, O_RDONLY);

    if (fd < 0)
        printf("open error\n");

    /* <unistd.h> */
    /* ssize_t read(int filedes, void *buf, size_t nbytes) */
    if (read(fd, buf, sizeof(buf)) < 0)
        printf("read error\n");

    printf("------after read------\n");
    printf("%s\n", buf);

    if (write(fd, buf2, sizeof(buf2)) < 0)
        printf("write error\n");

    printf("------after write-----\n");
    printf("%s\n", buf);

    close(fd);    /*成功返回0,出错返回-1*/
    exit(0);
}

void open_writeMode(const char *pathname)
{
    int fd;
    char buf[1024] = {0};
    const char *buf2 = "this is O_WRONLY";

    fd = open(pathname, O_WRONLY);

    if (fd < 0)
        printf("open error\n");
    if (read(fd, buf, sizeof(buf)) < 0)
        printf("read error\n");

    printf("------after read------\n");
    printf("%s\n", buf);

    if (write(fd, buf2, sizeof(buf2)) < 0)
        printf("write error\n");

    printf("------after write-----\n");
    printf("%s\n", buf);
}

/*
<unistd.h> 
off_t lseek(int filedes, off_t offset, int whence); 
*/
void open_lseek(const char *pathname)
{
    int fd;
    char buf[256] = {0};
    char buf2[] = "this is a hole";

    int prepos;
    off_t currpos; /*lseek返回值*/ 

    /*是用可读可写方式打开文件*/
    fd = open(pathname, O_RDWR);
    if (fd < 0)
        printf("open error\n");

    if (read(fd, buf, sizeof(buf)) < 0)
        printf("read error\n");

    /*获取读取的文件大小(bytes),+1是\n*/
    prepos = strlen(buf);
    /*将文件fd的偏移量设置为当前值加8,返回新的偏移量给currpos*/
    currpos = lseek(fd, 8, SEEK_CUR);
    /*strlen(buf)是14,有一个\n*/
    printf("prepos=%lu, currpos=%lld", strlen(buf), currpos);

    /*制造空洞文件*/
    ssize_t write_ret;
    write_ret = write(fd, buf2, sizeof(buf2));    /* write返回值,返回sizeof(buf2)*/
    printf("write_ret = %zd\n", write_ret);    /* %zd 对应size_t */
    if (write_ret < 0)
    {
        printf("write error\n");
    }
}

/*标准输入&标准输出:文件描述符0是标准输入,1是标准输出*/
/* 
UNIX <unistd.h>已经定义了宏值:
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
 */
void standard_io(const char *ptr_input)
{
    int n;
    char buf[1024] = {0};

    printf("%s\n", ptr_input);
    while ((n = read(STDIN_FILENO, buf, 1024)) > 0)
    {
        if (write(STDOUT_FILENO, buf, n) != n)
        {
            printf("write to stdout error\n");
        }
    }

    if (n < 0)
    {
        printf("read from stdin error\n");
    }
}
点击查看更多内容
75人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消