标签:pre 标准输入 c语言实现 返回 抽象 访问 img opera 文件名
操作系统(Operating System,简称OS)完成的工作比喻为两个角色:服务生和管家婆
两个重要命令:
man -k key1|grep key2|...
根据关键字检索系统调用grep -nr XXXX /usr/incldue
查找相关的宏定义,结构体定义,类型定义等其它知识点
man -k +函数名
搜索函数信息man +数字+函数
查到相关的命令和函数cat+文件名称
查看文本文件内容od +文件名称
查看二进制文件内容SEE ALSO
中得到相关系统调用的信息使用c语言实现who命令:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(char *filename, int falgs, mode_t mode);
/* 成功则返回新文件描述符,出错返回-1;
char *filename:函数将filename转换为一个文件描述符,并返回描述符数字;返回的描述符总是在进程中当前没有打开的最小描述符;
int flags:指明进程打算如何访问这个文件;
mode_t mode:指定了新文件的访问权限位。
*/
#include <unistd.h>
int close(int fd);
/* 成功则返回0,出错则为-1。 */
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t n);
/* 成功则返回读的字节数,若EOF则为0,若出错则为-1。 */
ssize_t write(int fd, const void *buf, size_t n);
/* 成功则返回写的字节数,若出错则为-1。 */
linux>ls>foo.txt
使得shell加载和执行ls程序,将标准输出重定向到磁盘文件foo.txt#include<unistd.h>
int dup2(int oldfd,int newfd);
/*返回,若成功则为非负的描述符,若出错则为-1*/
man head
和man tail
查询可知head、tail的作用分别是显示一个文件的前十行和后十行一、伪代码:
void main( ){
计数标志 count=0;
循环按字符读入文件
{
输出字符;
if(读入字符为回车符){cout++;}
if(count==10){退出循环}
}
}
void main(){
统计文件总行数n;
计数标志 count=0
循环按字符读入文件
{
if(读入字符为回车符){cout++;}
if(count>=n-10){
输出字符
}
}
}
二、产品代码
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc,char *argv[])
{
int count=0;
char ch;
int fd=0;
fd=open(argv[argc-1],O_RDONLY,0);
if(fd==-1){printf("Error!\n");exit(1);}
while(read(fd,&ch,1)!=0)
{
putchar(ch);
if(ch == ‘\n‘){count++;}
if(count == 10){break;}
}
close(fd);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int ct(int fd)
{
int count=0;
char c;
while(read(fd,&c,1)!=0)
{
if(c == ‘\n‘)
count++;
}
return count;
}
void mh(int fd,int n)
{
int count=0;
char c;
while (read(fd, &c, 1)!=0) {
if (c==‘\n‘) {
count++;
}
if (count>=n-10) {
putchar(c);
}
}
}
int main()
{
int fd=0,ft=0;
int count;
fd=open("aaa.txt",O_RDONLY);
count=ct(fd);
close(fd);
ft=open("aaa.txt",O_RDONLY);
mh(fd,count);
close(ft);
return 0;
}
运行截图:
2018-2019 20165215 《信息安全系统设计基础》第六周学习总结
标签:pre 标准输入 c语言实现 返回 抽象 访问 img opera 文件名
原文地址:https://www.cnblogs.com/fyss/p/9905259.html