zoukankan      html  css  js  c++  java
  • write() ,read();

    int main1(int argc ,char *argv[])
    {

      if(argc < 2 ) return 0;
      int fd = open(argv[1] , O_RDONLY);
      if(fd == -1)
      {
        printf("error is %s " , strerror(errno));
      }
      else
      {
        printf("fd = %d " ,fd);
        char buf[100];
        memset(buf , 0, 100);

        while(read(fd, buf, sizeof(buf)-1) > 0)//文件大的时候由于buf太小每次只读buf大小, sizeof(buf)-1表示每次不把buff读满留一个作为结尾防止最后一个字节乱码
        {
          printf("%s " , buf);
          memset(buf , 0, 100);//读完以后清空buf

        }

        

      }

      close(fd);

      return EXIT_SUCCESS;
    }

    int main(int argc ,char *argv[])
    {
      char *s = {"abc.txt"};
      int fd = open(s, O_RDWR|O_APPEND);//以读写追加的方式

      if(fd == -1)
      {
        printf("error is %s " , strerror(errno));
      }
      else
      {
        printf("sucess fd = %d " ,fd);
        char buf[100];  
        memset(buf, 0,100);
        strcpy(buf,"hello world! ");
        int i = write(fd, buf , strlen(buf));
      }

      close(fd);

    return 0;
    }

  • 相关阅读:
    [转]java 常用弹出框
    [转]ImageIcon icon 相对路径设置
    [转]『基本ASCII表和c语言运算表查询』
    [转]sqlmap技术手册
    [转]linux下怎么查看ssh的用户登录日志
    [转]Kali-linux安装之后的简单设置
    查看任意程序所连接的ip地址
    JS 闭包
    JS 中的 继承
    JS 原型的 理解
  • 原文地址:https://www.cnblogs.com/yuankaituo/p/4326505.html
Copyright © 2011-2022 走看看