『Shell编程』学习记录(2)
例1.文件io
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> int main(int argc, char **argv) {
printf("%s\n",argv[]);
int fdt,fds;
char buf[];
int num = ;
if ((fds = open("/etc/profile",O_RDONLY)) < ) {
printf("open fail\n");
}
if ((fdt = open(argv[],O_CREAT|O_TRUNC|O_RDWR) < ) {
printf("open fail\n");
return ;
}
while () {
if ((num = read(fds,buf,)) < ) {
printf("read fail\n");
}
if (write(fdt,buf,num) < ) {
printf("write fail\n");
return ;
}
if (num != ) {
break;
}
}
close(fds);
close(fdt);
return ;
}
① 这些常数的定义,在/usr/include/bits/fcntl.h
#define O_ACCMODE 0003
#define O_RDONLY 00
#define O_WRONLY 01
#define O_RDWR 02
#define O_CREAT 0100 /* not fcntl */
#define O_EXCL 0200 /* not fcntl */
#define O_NOCTTY 0400 /* not fcntl */
#define O_TRUNC 01000 /* not fcntl */
#define O_APPEND 02000
#define O_NONBLOCK 04000
#define O_NDELAY O_NONBLOCK
#define O_SYNC 010000
#define O_FSYNC O_SYNC
#define O_ASYNC 020000
可以看出,每个常数都对应一位。所以按位或得到的值,每多或上一个常数,就是把对应的一位置1。
O_TRUNC在open()应首先删除文件中的内容,然后开始编写。
file.txt中包含ASCII '11',它应该做的是读取它并将其覆盖为'8',文件最终为'8'。
代码的目标是读取文件中的数字,将其减3,然后仅使用系统调用将该数字放回文件中。
#include <unistd.h>
#include <fcntl.h> int main(int argc, char*argv[]){ int fp = open("file.txt", O_RDONLY);
char c1, c2, c3='\n'; read(fp, &c1, );
read(fp, &c2, );
close(fp);
fp = open("file.txt", O_TRUNC | O_WRONLY); if (c2 == '\n')
c1 -= ;
else {
if (c2 >= '' && c2 <= '' ) {
c1--;
c2 += ;
}
else
c2 -= ; }
if (c1 != '')
write(fp,&c1,);
if (c2 != '\n')
write(fp,&c2,);
write(fp,&c3,);
return ;
}:
②
void open (const char* filename,
ios_base::openmode mode = ios_base::in | ios_base::out);
对filename进行后面的操作组合
参考:http://www.cplusplus.com/reference/fstream/basic_fstream/open/
例程的功能是将 /etc/profile 文件每20个字节进行读取到 arg[1] (没有则重新创建,有则进行内容覆盖)文件中。
例2.进程间通信
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> int main(int argc, char **argv) {
int pipe_fd[];
pid_t pid;
char buf_r[]; char* p_wbuf = "hello world!";
int r_num = ; memset(buf_r, , sizeof(buf_r)); if (pipe(pipe_fd) < ) {
printf("pipe create error\n");
return -;
}
if ((pid = fork()) == ) {
close(pipe_fd[]);
sleep();
if ((r_num = read(pipe_fd[], buf_r, )) > ) {
printf("%d numbers read from the pipe is \" %s \"\n", r_num, buf_r);
}
close(pipe_fd[]);
exit();
}
else if (pid > ) {
close(pipe_fd[]);
if (write(pipe_fd[], p_wbuf, strlen(p_wbuf)) != -) {
printf("parent write \" %s \" success!\n", p_wbuf);
}
close(pipe_fd[]);
sleep();
waitpid(pid, NULL, );
exit();
} }
①
void * memset ( void * ptr, int value, size_t num );
将ptr指的内存的num个字节用value设置set。
/* memset example */
#include <stdio.h>
#include <string.h> int main ()
{
char str[] = "almost every programmer should know memset!";
memset (str,'-',);
puts (str);
return ;
}
Output:
------ every programmer should know memset! |
②
pipe(filedes)的功能: 建立一无名管道。管道建立后,写进程将数据写入文件 filedes[1],读进程 从文件 filedes[0]中读数据,从而实现读/写进程的管道通信。
例程的功能是将p_wbuf指向的100个内存单元通过pipe传给buf_r[100]。
『Shell编程』学习记录(2)的更多相关文章
- 『Shell编程』学习记录(1)
例1. $ cat ex1 date pwd cd .. $ bash ex1 # 运行,显示当前日期和当前目录,但没有执行返回上级目录,因为执行的时候终端会产生一个子shell(类似于C语言调用函数 ...
- 『C编程』学习笔记(1)
size_t类型详解: #include <cstddef> #include <iostream> #include <array> int main() { s ...
- Linux 与 unix shell编程指南——学习笔记
第一章 文件安全与权限 文件访问方式:读,写,执行. 针对用户:文件属主,同组用户,其它用户. 文件权限位最前面的字符代表文件类型,常用的如 d 目录:l 符号链 ...
- linux shell编程进阶学习(转)
第一节:基础 ls -lh ——可以用户友好的方式看到文件大小 file 文件名 ——查看文件类型 stat 文件名 ——查看文件当前状态 man 命令/函数名 ——查看详细的帮助文档 man中看某 ...
- Linux下C语言编程基础学习记录
VIM的基本使用 LINUX下C语言编程 用gcc命令编译运行C语言文件 预处理阶段:将*.c文件转化为*.i预处理过的C程序. 编译阶段:将*.i文件编译为汇编代码*.s文件. 汇编阶段:将*.s ...
- Linux Unix shell 编程指南学习笔记(第三部分)
第十三章 登陆环境 登陆系统时.输入username和password后.假设验证通过.则进入登录环境. 登录过程 文件/etc/passwd $HOME.profile 定制$HOME.profi ...
- 《灰帽Python-黑客和逆向工程师的Python编程》学习记录
ctypes是Python语言的一个外部库,提供和C语言兼容的数据类型,可以很方便的调用C DLL中的函数. 操作环境:CentOS6.5 Python版本:2.66 ctypes是强大的,强大到本书 ...
- android adb shell and monkey 学习记录
Monkey环境: android SDK and JDK SDK目录下的platform-tools和tools目录要配置环境变量 查看版本: ADB 的安装这里就不多说了,输入以下命令有如下提示就 ...
- Linux Unix shell 编程指南学习笔记(第四部分)
第十六章 shell脚本介绍 此章节内容较为简单,跳过. 第十七章 条件測试 test命令 expr命令 test 格式 test condition 或者 [ conditio ...
随机推荐
- angular开发环境搭建及新建项目
最近一个星期准备学习一下angular前端框架,因为之前在学习abp框架的时候,都要求前端要掌握angular,所以不得不回来恶补一下了,学习的过程有时间的话会记录在这里,方便以后复习. 闲言少叙,下 ...
- ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第一天
文章大纲 一.课程介绍二.淘淘商城基本介绍三.后台管理系统工程结构与搭建四.svn代码管理五.项目源码与资料下载六.参考文章 一.课程介绍 1. 课程大纲 一共14天课程(1)第一天:电商行业的背 ...
- Spring boot打包war包
1.设置打包的类型(war/jar) 在pom.xml里设置 <packaging>war</packaging> 2.移除嵌入式tomcat插件 //在pom.xml里找到s ...
- Android Glide详细使用教程
此处我只是做个记录,后边再补充 原文地址:http://www.jufanshare.com/content/35.html 这篇文章写的比较清楚,还附有Demo代码.算是不错的Android Gli ...
- Python之路【第八篇】:Python模块
阅读目录 一.模块和包 模块(module)的概念: 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分别放到 ...
- spring Boot环境下dubbo+zookeeper的一个基础讲解与示例
一,学习背景 1. 前言 对于我们不管工作还是生活中,需要或者想去学习一些东西的时候,大致都考虑几点: a) 我们为什么需要学习这个东西? b) 这个东西是什么? c) ...
- Java 枚举类详解
1. 枚举类定义 在某些情况下,一个类的对象是有限而且固定的,比如季节类,它只有4个对象,这种实例有限而且固定的类,在Java里被称为枚举类. 2. 早期实现枚举的方式 public static f ...
- Markdown 插入图片技巧
在使用Markdown编写博客或文件的时候,经常会需要插入图片.如果使用Markdown语法,我们会发现调整图片的大小会是个问题. 所以推荐使用另一种办法插入图片,使用HTML语法,示例如下: < ...
- Identity Server 4 - Hybrid Flow - 保护API资源
这个系列文章介绍的是Identity Server 4 的 Hybrid Flow, 前两篇文章介绍了如何保护MVC客户端, 本文介绍如何保护API资源. 保护MVC客户端的文章: https://w ...
- springBoot(13)---整合Druid实现多数据源和可视化监控
SpringBoot整合Druid实现多数据源和可视化监控 先献上github代码地址:https://github.com/yudiandemingzi/springboot-manydatasou ...