zoukankan      html  css  js  c++  java
  • UNIX不带缓存的IO函数

       大多数UNIX文件I/O操作只需用到5个函数:open、read、write、lseek、close,上述五个函数经常被称为不带缓存的I/O;不带缓存指的是每个read和write都调用内核中的一个系统调用;这些不带缓存的I/O函数不是ANSI C的组成部分,但是是POSIX.1和XPG3的组成部分。
      下面我们来看看这些函数的应用,打开一个文件,读出1024字节,并输出内容。

     1 #include <stdlib.h>
     2 #include <stdio.h>
     3 #include <fcntl.h>
     4 #include <unistd.h>
     5 int main(int argc, char* argv[])
     6 {
     7     int fd, nbytes;
     8     char buf[1025];
     9     
    10     if(argc < 2)
    11     {
    12         printf("Usage: %s filename\n", argv[0]);
    13         exit(0);
    14     }
    15     if((fd = open(argv[1], O_RDONLY)) < 0)
    16     {
    17         printf("Error:open %s failed\n", argv[1]);
    18         exit(0);
    19     }
    20     if((nbytes = read(fd, buf, sizeof(buf)) - 1) < 0)
    21     {
    22         printf("Error:read %s failed\n", argv[1]);
    23         exit(0);
    24     }
    25     buf[1024] = 0;
    26     printf("Read %d from %s\n", nbytes, argv[1]);
    27     printf("%s\n",buf);
    28     return 0;
    29 }
    ---
    可以转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明
  • 相关阅读:
    .net 日期格式化
    grunt 上手
    设计模式的认识
    顺时针打印矩阵
    WCF 框架运行时类图
    Python闭包详解
    软件用了那些技术
    zoj 1610 Count the Colors(线段树延迟更新)
    快速提高自己的技术的办法?有两个方法
    纯win32实现PNG图片透明窗体
  • 原文地址:https://www.cnblogs.com/null00/p/2065104.html
Copyright © 2011-2022 走看看