手记

UNIX-文件IO函数使用范例

/*
文件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人推荐
随时随地看视频
慕课网APP