linux 实现cat命令
2015-09-13 16:39
417 查看
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main(int argc,char **argv)
{
int fd;
char buf[64];
int ret;
fd=open(argv[1],O_RDONLY); /*以只读的方式打开文件*/
if(fd<0)
{
perror("open()");
return -1;
}
while(1){
bzero(buf,64);
ret=read(fd,buf,64); /*从文件中读数据,*/
if(ret<=0)
{
break;
}
write(1,buf,ret); /*打印出来*/
}
close(fd);
return 0;
}
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main(int argc,char **argv)
{
int fd;
char buf[64];
int ret;
fd=open(argv[1],O_RDONLY); /*以只读的方式打开文件*/
if(fd<0)
{
perror("open()");
return -1;
}
while(1){
bzero(buf,64);
ret=read(fd,buf,64); /*从文件中读数据,*/
if(ret<=0)
{
break;
}
write(1,buf,ret); /*打印出来*/
}
close(fd);
return 0;
}
相关文章推荐
- /usr/bin/ld: warning: libswresample.so.1, needed by /usr/lib/gcc/i686-linux-gnu/4.9/../../../../lib/
- windows和linux的文件传输方法
- 在linux下安装vnc
- Dart SDK在Linux上的下载及配置
- [Linux] 进程同步和进程通信的方式
- linux errno code
- linux 忘记root(这里以centos 6.5为例)密码的解决办法
- Linux网络通信工具
- linux下安装.deb和.rpm软件包的方法
- 新一代 Linux 文件系统 btrfs 简介
- centos6.5安装crmsh
- Linux进程理解与实践(四)wait函数处理僵尸进程
- (Tutorial) [Linux VPS] Making a Lithium-Based v111 + MapleStory Server on a Linux VPS
- Writing device drivers in Linux: A brief tutorial
- linux端口映射
- Linux文件管理系统
- Linux字符设备驱动之中断按键
- Linux开启ssh服务
- Linux下搭建DHCP服务
- linux虚拟机解决校园网上网方案