Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2242066
  • 博文数量: 395
  • 博客积分: 10994
  • 博客等级: 上将
  • 技术积分: 5586
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-17 19:49
文章存档

2014年(1)

2013年(10)

2012年(74)

2011年(303)

2010年(7)

分类: C/C++

2011-05-21 11:38:16

注:记得学习open,read,write函数已经是半年以前的事情了,可是一直没有自己编过,这次是一个网友除了问题,让我看看,他出现了问题,说是会overflow可是我没有呀(不过后来发现是她是在ubuntu下,用的普通用户,没有权限,呵呵),也不知道咋回事的,很正常,下面是代码和运行时的过程

 

#include "stdio.h"

#include "stdlib.h"

#include "fcntl.h"

#include "sys/types.h"

#include "sys/stat.h"

 

int main(void){

  int fp1,fp2;

  int size;

  char buffer[80];

  if((fp1=open("abc.txt",O_RDONLY))<0){

  printf("this file is not exit!");

  exit(0);

  }

  else

        printf("file open success\n");

  if((fp2=open("cd.txt",O_WRONLY|O_CREAT))<0){

  printf("overflow!");

  exit(0);

  }

  else

        printf("file creat success\n");

 

  while(size=read(fp1,buffer,sizeof(buffer))){

      write(fp2,buffer,size);

}

  close(fp1);

  close(fp2);

}

 

 

[root@bogon tmp]# ls

abc.txt  file  file.c

[root@bogon tmp]# ./file

file open success

file creat success

[root@bogon tmp]# ls

abc.txt  cd.txt  file  file.c

[root@bogon tmp]# cat abc.txt

zhangzhengzhouqingogne   xueyuan

[root@bogon tmp]# cat cd.txt

zhangzhengzhouqingogne   xueyuan

[root@bogon tmp]#

 

阅读(1009) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~