『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 ...
随机推荐
- C#开发APP,ToolBar控件在Smobiler中的使用方式【附案例源码】——Smobiler移动开发平台
控件说明 底部工具栏控件. 效果演示 其他效果 该界面为仿淘宝UI制作的一个简单的UI模板,源码获取方式请拉至文章末尾. 特色属性 属性 属性说明 Direction(相对布局) 容器主轴方向. Fl ...
- 设计模式 | 建造者模式/生成器模式(builder)
定义: 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. 结构:(书中图,侵删) 一个产品类 一个指定产品各个部件的抽象创建接口 若干个实现了各个部件的具体实现的创建类 一个 ...
- 如何让div中的table水平居中
<div style="text-align:center"> <table border="1" cellpadding="3&q ...
- Android resource compilation failed
报错:Android resource compilation failed D:\android\EasySports\app\build\intermediates\incremental\mer ...
- 解决mysql中只能通过localhost访问不能通过ip访问的问题
解决mysql中只能通过localhost访问不能通过ip访问的问题 原因是没开权限 SELECT * FROM USER WHERE USER='root'; grant all privilege ...
- 如何使用Android Studio在安卓平台对Unity开发的应用进行性能检查?
0x00 前言 大家常常会抱怨安卓平台没有一个统一.好用的性能检查工具.不能像iOS的instrument那样方便. 图片来自:Instruments Help 比如,Unity Blog在3年前就已 ...
- Android Studio导出JavaDoc时中文乱码问题解决
导出过程中,如果出现JavaDoc中文乱码的问题,可以在Other command line arguments栏目添加命令参数:-encoding UTF-8 -charset UTF-8(如果是G ...
- ubuntu修改键盘映射
code {margin: 0;padding: 0;font-size: 100%;word-break: normal;background: transparent;border: 0;}ol ...
- oracle 常用索引分析,使用原则和注意事项
本文参考: https://www.cnblogs.com/wishyouhappy/p/3681771.html https://blog.csdn.net/weivi001/article/det ...
- web服务器,验证码,Xftp使用方法
IIS操作步骤 直接装的wamp 腾讯云主机控制台 安全组里可以配置要开放的端口 关闭防火墙 (C:\wamp\bin\apache\Apache2.4.4) 打开httpd.conf文件 requi ...