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\n", 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\n";

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] = '\0';
        
    close(wr_fd);
    close(rd_fd);
    printf("str is: %s\n", 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\n";

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\n", 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\n", 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 ...\n");
        break;
        case O_WRONLY:
            printf("Write only ...\n");
        break;
        case O_RDWR:
            printf("Read And Write ...\n");
        break;
        default:
            printf("Do't know...\n");
        break;
    }

    if (var & O_APPEND) {
        printf("And Append...\n");
    }
    if (var & O_NONBLOCK) {
        printf("And Blocking...\n");
    }
    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\n", size.ws_row, size.ws_col);
    return 0;
}
复制代码

运行结果:

36 rows, 121 columns

 



如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
posted @   夜行过客  阅读(467)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
历史上的今天:
2016-10-25 java.net.Socket/java.net.ServerSocket-TCP Socket编程
2016-10-25 转:Java NIO系列教程(一)Java NIO 概述
2016-10-25 Java-Java中System.arraycopy() 和 Arrays.copyOf()两者之间的区别
点击右上角即可分享
微信分享提示