先给出源码

//fileio.c
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
void print(int fd)
{
int i,len;
char buf[10];
len=read(fd,buf,10);//len是成功读入的字节数,每read一次,文件偏移量就会偏移10个字节位置
while(len>0)//循环打印,直至读到文件尾部了
{
for(i=0;i<len;i++)
{
printf("%c",buf[i]);
}
len=read(fd,buf,10);
}
}
void prin()
{
char buf[1024];//我对指针还不熟,不过换成指针应该会好点吧
while(1)//死循环打印字符串
{
scanf("%s",buf);
puts(buf);
}
}
int main(int argc,char *argv[])//argc参数个数,注意为1时代表没有参数(只有程序名字),argv[]是指参数,argv[1]指第一个参数,argv[2]指第二个参数。。。
{
int fd,i;
if(argc==1)//没有参数,跳到prin函数,
{
prin();
return 0;
}
for(i=1;i<=argc-1;i++)//因为argc为2时才表示有一个参数啊
{
fd=open(argv[i],O_RDWR);//以可写可读方式打开
if(fd==-1)//打开失败
{
perror("Error");//显示错误信息
}
else
{
print(fd);//打开成功跳转print函数
}
close(fd);//记得打开之后关闭文件描述符
}
return 0;
}

下面给演示一下我的运行结果

[root@bogon mycode]# gcc fileio.c
[root@bogon mycode]# ./a.out
linux
linux
ok
ok
^C
[root@bogon mycode]# ./a.out test.txt
ok
that is fine
linux
[root@bogon mycode]# ./a.out test.txt fileio.c
ok
that is fine
linux
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
void print(int fd)
{
int i,len;
char buf[10];
len=read(fd,buf,10);
while(len>0)
{
for(i=0;i<len;i++)
{
printf("%c",buf[i]);
}
len=read(fd,buf,10);
}
}
void prin()
{
char buf[1024];
while(1)
{
scanf("%s",buf);
puts(buf);
}
}
int main(int argc,char *argv[])
{
int fd,i;
if(argc==1)
{
prin();
return 0;
}
for(i=1;i<=argc-1;i++)
{
fd=open(argv[i],O_RDWR);
if(fd==-1)
{
perror("Error");
//continue;
}
else
{
print(fd);
}
close(fd);
}
return 0;
}
[root@bogon mycode]# ./a.out nothisfile.txt test.txt
Error: No such file or directory
ok
that is fine
linux
[root@bogon mycode]#

这个小程序免不了有bug,大神们发现了的话可以留言交流一下,谢谢

使用C语言简单模拟Linux的cat程序的更多相关文章

  1. 通过简单的Linux内核启动程序代码窥探操作系统的启动原理

    作者:吴乐  山东师范大学 <Linux内核分析> 孟宁 MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.程序设计与分析 ...

  2. Linux 内核 链表 的简单模拟(1)

    第零章:扯扯淡 出一个有意思的题目:用一个宏定义FIND求一个结构体struct里某个变量相对struc的编移量,如 struct student { int a; //FIND(struct stu ...

  3. Linux 内核 链表 的简单模拟(2)

    接上一篇Linux 内核 链表 的简单模拟(1) 第五章:Linux内核链表的遍历 /** * list_for_each - iterate over a list * @pos: the & ...

  4. Linux内核分析第三周学习总结:构造一个简单的Linux系统MenuOS

    韩玉琪 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.Linux内 ...

  5. 以Qemu模拟Linux,学习Linux内核

    文章名称:以Qemu模拟Linux,学习Linux内核作      者:five_cent文章地址:http://www.cnblogs.com/senix/archive/2013/02/21/29 ...

  6. 运用Python语言编写获取Linux基本系统信息(二):文件系统使用情况获取

    本文跟着上一篇文章继续写,上一篇文章的链接 运用Python语言编写获取Linux基本系统信息(一):获得Linux版本.内核.当前时间 一.随便说说 获取文件系统使用情况的思路和上一篇获取主要系统是 ...

  7. Linux下简单的取点阵字模程序

    源:Linux下简单的取点阵字模程序 Linux操作系统下进行简单的图形开发,经常会用到取字模的软件,但是Linux并没有像Windows下的小工具可用,我们也并不希望为了取字模而频繁地切换操作系统. ...

  8. Linux内核分析— —构造一个简单的Linux系统MenuOS(20135213林涵锦)

    Linux内核分析— —构造一个简单的Linux系统MenuOS 实验内容 Linux内核的启动过程,从start_kernel到init进程启动 使用实验楼的虚拟机打开shell cd LinuxK ...

  9. python--selenium简单模拟百度搜索点击器

    python--selenium简单模拟百度搜索点击器 发布时间:2018-02-28 来源:网络 上传者:用户 关键字: selenium 模拟 简单 点击 搜索 百度 发表文章摘要:用途:简单模拟 ...

随机推荐

  1. Linux学习 : Socket 网络编程入门

    一.socket()函数 int socket(int domain, int type, int protocol); domain:即协议域,又称为协议族(family).常用的协议族有,AF_I ...

  2. 实力封装:Unity打包AssetBundle(四)

    →→前情提要:窗口初现←← 让用户选择要打包的文件 时至今日,我们选择打包文件的方式依然是在Project面板或Hierarchy面板中用鼠标点选.现在既然有了窗口,我们自然希望可以将所有文件罗列在窗 ...

  3. Java不同类型字符转换String/int/Float/////

    1.int & String int i=5678;String s=""; int->String: s=i+"";或 s=String.val ...

  4. final视频

    作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2476] 视频链接[https://v.youku.com/v_show/id_ ...

  5. L259 合同

    We are satisfied with the terms of this contract for the most part, but we feel that your terms of p ...

  6. day 32 子进程的开启 及其用法

    开启两种子进程的两种方式# # # 1 传统方式# from multiprocessing import Process# import time# def task(name):# print ( ...

  7. golang---map类型

    map 类似其它语言中的哈希表或字典,以key-value形式存储数据 key必须是支持==或!=比较运算的类型,不可以是函数.map或slice Map查找比线性搜索快很多,但比使用索引访问数据的类 ...

  8. Maven下用MyBatis Generator生成文件

    使用Maven命令用MyBatis Generator生成MyBatis的文件步骤如下: 1.在mop文件内添加plugin <build> <finalName>KenShr ...

  9. 【Python】socket编程-2

    #练习3:TCP协议+while循环 服务端: import socket #socket模块 import sys reload(sys) sys.setdefaultencoding(" ...

  10. promise、async和await之执行顺序

    async function async1(){ console.log('async1 start') await async2() console.log('async1 end') } asyn ...