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);
}
#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);
}
相关文章推荐
- 两个有序数组归并为一个有序数组(去重)
- C++模板学习
- c++ boost--------------boost::progress_timer小试。
- C++中如何建立一个顺序表
- C++中如何建立一个顺序表
- C++临时对象
- Barcode Xpress如何在Visual C++中作为COM对象使用(2)
- C语言打印圣诞树
- 编程基础------C和C++函数的相互引用----C++中extern “C”含义
- LIB和DLL的区别与使用
- VC++ win32 多线程 一边画圆一边画矩形
- 走进C++程序世界------继承和派生
- C++ 函数指针
- A fast lock-free queue for C++
- C++ 中通过GetAdaptersInfo获取网卡配置和Ip地址信息
- C语言中的while循环和for循环
- C语言左移右移
- C++的单例模式
- 一个c++程序员的读书笔记(一)
- 关于c++的内联函数