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\n" , strerror(errno));
  }
  else
  {
    printf("fd = %d\n" ,fd);
    char buf[100];
    memset(buf , 0, 100);

    while(read(fd, buf, sizeof(buf)-1) > 0)//文件大的时候由于buf太小每次只读buf大小, sizeof(buf)-1表示每次不把buff读满留一个作为结尾防止最后一个字节乱码
    {
      printf("%s\n" , 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\n" , strerror(errno));
  }
  else
  {
    printf("sucess fd = %d\n" ,fd);
    char buf[100];  
    memset(buf, 0,100);
    strcpy(buf,"hello world!\n");
    int i = write(fd, buf , strlen(buf));
  }

  close(fd);

return 0;
}

随机推荐

  1. C#- 操作Ini文件

    以前习惯了使用.NET中的WEB.CONFIG或者APP.CONFIG,最近在做项目的时候遇到了些问题,发现没办法使用这些CONFIG文件.一开始我的做法是建一个文本文件,自己定规律,自己写方法去写新 ...

  2. cocos2d-x 的CCObject与autorelease 之深入分析

    转自: http://blog.csdn.net/honghaier/article/details/8160519 CCObject.h: #ifndef __CCOBJECT_H__ #defin ...

  3. github创建tag

    最近在使用github给新的jQuery插件:滚动高亮 添加版本库的时候,看到很多github上的项目都有这个标签,可以清晰快速的找到每个不同的版本,非常方便以后查找以及使用.于是我就在继上一次的:将 ...

  4. SQL中使用WITH AS提高性能

    本文内容一部分来自:http://wudataoge.blog.163.com/blog/static/80073886200961652022389/ 一.WITH AS的含义     WITH A ...

  5. Bash For Loop Examples for Your Linux Shell Scripting--ref

    There are two types of bash for loops available. One using the “in” keyword with list of values, ano ...

  6. linux atime ctime mtime

    touch testtime 1. stat testtime[为文件名] 可以查看这个文件名的三者状态 2.ll testtime;ll --time=atime testtime ;ll --ti ...

  7. Git使用完全解析(一)

    是时候来系统的介绍一下Git了.毫无疑问,Git是目前最优秀的分布式版本控制工具,木有之一,可是我见到的很多人还是不会用,我的老东家每天忍受着SVN带来的痛苦,却迟迟不愿切换到Git上,个人感觉,许多 ...

  8. hysdk代码解析

    navigator 1. navigator.userAgent 浏览器的用户代理字符串 2. navigator.platform 浏览器所在的系统平台 window 1. window.devic ...

  9. 安卓蓝牙技术Bluetooth使用流程(Bluetooth详解)

    一:蓝牙设备之间的通讯首要包含了四个进程 设置蓝牙设备 寻觅局域网内也许或许匹配的设备 衔接设备 设备之间的数据传输 二:详细编程完结 1. 发动蓝牙功用 首要经过调用静态办法getDefaultAd ...

  10. iOS之苹果和百度地图的使用

    iOS中使用较多的3款地图,google地图.百度地图.苹果自带地图(高德).其中苹果自带地图在中国使用的是高德的数据.苹果在iOS 6之后放弃了使用谷歌地图,而改用自家的地图.在国内使用的较多的就是 ...