您的位置:首页 > 编程语言 > C语言/C++

C语言示例

2013-09-25 00:38 232 查看
//文件的操作

#include<stdlib.h>

#include<unistd.h>

#include<stdio.h>

#include<sys/stat.h>

#include<sys/types.h>

#include<fcntl.h>

#include<error.h>

int main(){

int fdsrc,fdnull,fdtmp,numbytes;

int flags=O_CREAT|O_TRUNC|O_WRONLY;

char buf[10];

if((fdsrc=open("1th.c",O_RDONLY))<0){

printf("%d\n",__LINE__);

perror("open 1th.c");

exit(EXIT_FAILURE);

}

if((fdnull=open("/dev/null",O_WRONLY))<0){

printf("%d\n",__LINE__);

close(fdsrc);

perror("open /dev/null");

exit(EXIT_FAILURE);

}

if((fdtmp=open("/tmp/foo.bar",flags,0644))<0){

printf("%d\n",__LINE__);

close(fdsrc);

close(fdnull);

perror("open /tmp/foo.bar");

exit(EXIT_FAILURE);

}

while((numbytes=read(fdsrc,buf,10))>0){

write(fdnull,buf,numbytes);

write(fdtmp,buf,numbytes);

write(1,buf,numbytes);

}

close(fdsrc);

close(fdtmp);

close(fdnull);

exit(EXIT_SUCCESS);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: