1.文件I/O

一. open()&close()

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


int main()
{
    
    int fd;
    fd = open("abc.txt", O_CREAT|O_RDWR, 0777); // 若flag中使用了O_CREAT,则需要指定第三个参数访问权限
    if (fd < 0)
        printf("file create failure");
    printf("current fd is: %d
", fd);
    close(fd);
    return 0;
}

二.read()&write()

write.c

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

static const char *str = "http://www.baidu.com
";

int main()
{
    int fd;
    fd = open("cde.txt", O_CREAT|O_RDWR|O_APPEND, 0777);
        
    write(fd, str, strlen(str));
    close(fd);
    return 0;
}

read.c

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

int main()
{
    
    char tmp[30];
    char str[1024]; 
    
    int wr_fd;
    wr_fd = open("aaa.txt", O_CREAT|O_WRONLY|O_APPEND, 0777);
    
    int rd_fd;
    rd_fd = open("cde.txt", O_RDONLY);
    
    int total = 0, len;
    while((len = read(rd_fd, tmp, 30))) {
        strncpy(str+total, tmp, len);
        total+=len;
    }
    str[total-1] = '';
        
    close(wr_fd);
    close(rd_fd);
    printf("str is: %s
", str);
    return 0;
}

运行结果:

str is: http://www.baidu.com
http://www.taobao.com
http://www.qq.com
http://www.dota2.com.cn
http://www.tmall.com
http://www.jd.com
http://www.apple.com
http://www.microsoft.com

三.lseek() 移动文件读写指针

使用lseek插入文件内容

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

static const char *str = "http://www.ibm.com
";

int main()
{
    
    int fd;
    off_t offset;
    fd = open("cde.txt", O_RDWR);
    offset = lseek(fd, 0, SEEK_END);
    
    write(fd, str, strlen(str));
    close(fd);    
    printf("cur offset is: %d
", offset);
    return 0;
}

运行结果:

http://www.qq.com
http://www.dota2.com.cn
http://www.tmall.com
http://www.jd.com
http://www.apple.com
http://www.microsoft.com
http://www.ibm.com
http://www.ibm.com

使用lseek计算文件大小

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


int main()
{

    int fd;
    off_t offset;
    
    fd = open("cde.txt", O_RDWR);
    
    offset = lseek(fd, 0, SEEK_END);
    printf("cur file size is: %d
", offset);
    close(fd);
    return 0;
}

运行结果:

cur file size is: 208

-rwxrwxr-x 1 yongdaimi yongdaimi 208 Jan 29 00:54 cde.txt

四.fcntl()

使用fcntl()获得文件的flag标志

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

int main()
{
    
    int fd;
    if ((fd = open("hh.txt", O_RDWR | O_CREAT | O_EXCL, 0664)) == -1) {
        perror("open error");
        exit(1);
    }    
    
    int var;
    if ((var = fcntl(fd, F_GETFL, 0)) < 0) {
        perror("fcntl error");
        close(fd);
        exit(1);
    }
    
    switch(var & O_ACCMODE) {
        case O_RDONLY:
            printf("Read only ...
");
        break;
        case O_WRONLY:
            printf("Write only ...
");
        break;
        case O_RDWR:
            printf("Read And Write ...
");
        break;
        default:
            printf("Do't know...
");
        break;
    }

    if (var & O_APPEND) {
        printf("And Append...
");
    }
    if (var & O_NONBLOCK) {
        printf("And Blocking...
");
    }
    if (close(fd) == -1) {
        perror("close error");
    }

    return 0;
}

运行结果:

Read And Write ...

五.ioctl()

使用TIOCGWINSZ命令获得终端设备的窗口大小

#include <sys/ioctl.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>


int main()
{

    struct winsize size;

    if (isatty(STDOUT_FILENO) == 0)
        exit(1);
    if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
        perror("ioctl TIOCGWINSZ error");
        exit(1);
    }
    printf("%d rows, %d columns
", size.ws_row, size.ws_col);
    return 0;
}

运行结果:

36 rows, 121 columns

原文地址:https://www.cnblogs.com/yongdaimi/p/7732575.html