例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_TRUNCopen()应首先删除文件中的内容,然后开始编写。
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 ;
}:

参考:https://stackoverflow.com/questions/49040262/o-trunc-in-system-call-open-not-actually-deleting-contents-of-file

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)的更多相关文章

  1. 『Shell编程』学习记录(1)

    例1. $ cat ex1 date pwd cd .. $ bash ex1 # 运行,显示当前日期和当前目录,但没有执行返回上级目录,因为执行的时候终端会产生一个子shell(类似于C语言调用函数 ...

  2. 『C编程』学习笔记(1)

    size_t类型详解: #include <cstddef> #include <iostream> #include <array> int main() { s ...

  3. Linux 与 unix shell编程指南——学习笔记

    第一章    文件安全与权限 文件访问方式:读,写,执行.     针对用户:文件属主,同组用户,其它用户.     文件权限位最前面的字符代表文件类型,常用的如         d 目录:l 符号链 ...

  4. linux shell编程进阶学习(转)

    第一节:基础 ls -lh  ——可以用户友好的方式看到文件大小 file 文件名 ——查看文件类型 stat 文件名 ——查看文件当前状态 man 命令/函数名 ——查看详细的帮助文档 man中看某 ...

  5. Linux下C语言编程基础学习记录

    VIM的基本使用  LINUX下C语言编程 用gcc命令编译运行C语言文件 预处理阶段:将*.c文件转化为*.i预处理过的C程序. 编译阶段:将*.i文件编译为汇编代码*.s文件. 汇编阶段:将*.s ...

  6. Linux Unix shell 编程指南学习笔记(第三部分)

    第十三章  登陆环境 登陆系统时.输入username和password后.假设验证通过.则进入登录环境. 登录过程 文件/etc/passwd $HOME.profile 定制$HOME.profi ...

  7. 《灰帽Python-黑客和逆向工程师的Python编程》学习记录

    ctypes是Python语言的一个外部库,提供和C语言兼容的数据类型,可以很方便的调用C DLL中的函数. 操作环境:CentOS6.5 Python版本:2.66 ctypes是强大的,强大到本书 ...

  8. android adb shell and monkey 学习记录

    Monkey环境: android SDK and JDK SDK目录下的platform-tools和tools目录要配置环境变量 查看版本: ADB 的安装这里就不多说了,输入以下命令有如下提示就 ...

  9. Linux Unix shell 编程指南学习笔记(第四部分)

    第十六章  shell脚本介绍 此章节内容较为简单,跳过. 第十七章   条件測试 test命令 expr命令 test  格式  test  condition     或者  [ conditio ...

随机推荐

  1. 来聊一聊不low的Linux命令——find、grep、awk、sed

    前几天面试,被一位面试官嫌弃了"你的Linux命令有点low".被嫌弃也挺正常的,因为我的简历写的我自己都有点看不下去:了解Linux常用命令,如ls,tail -f等命令,基本满 ...

  2. vue中路由按需加载的几种方式

    使用vue-cli构建项目后,我们会在Router文件夹下面的index.js里面引入相关的路由组件,如: import Hello from '@/components/Hello' import ...

  3. Kafka Ecosystem(Kafka生态)

    http://kafka.apache.org/documentation/#ecosystem https://cwiki.apache.org/confluence/display/KAFKA/E ...

  4. AI应用开发实战 - 手写算式计算器

    扩展手写数字识别应用 识别并计算简单手写数学表达式 主要知识点 了解MNIST数据集 了解如何扩展数据集 实现手写算式计算器 简介 本文将介绍一例支持识别手写数学表达式并对其进行计算的人工智能应用的开 ...

  5. .NET WebAPI中使用Session使用

    问题及其解决方案: 今天做项目的时候因为需要编写一个短信验证码的接口我需要在我的后台.net webapi中存入我随机生成的短信验证码方便与前端传递过来的数据对比,所以决定使用session做缓存.但 ...

  6. ios的跨站脚本限制

    概述 项目中碰到一个问题,就是在ios机上,用iframe内嵌的网页有时需要登录,有时候又不需要登录.查找了半天,终于发现是ios的跨站脚本限制导致的.这里就来介绍下跨站脚本限制,供以后开发时参考,相 ...

  7. IDEA指定.class文件输出位置

    1.File > Project Structure > Project > Project compiler output  项目中的默认编译输出总目录 2.我习惯于把.class ...

  8. 外行人都能看懂的SpringCloud,错过了血亏!

    一.前言 只有光头才能变强 认识我的朋友可能都知道我这阵子去实习啦,去的公司说是用SpringCloud(但我觉得使用的力度并不大啊~~)... 所以,这篇主要来讲讲SpringCloud的一些基础的 ...

  9. python学习第七讲,python中的数据类型,列表,元祖,字典,之元祖使用与介绍

    目录 python学习第七讲,python中的数据类型,列表,元祖,字典,之元祖使用与介绍 一丶元祖 1.元祖简介 2.元祖变量的定义 3.元祖变量的常用操作. 4.元祖的遍历 5.元祖的应用场景 p ...

  10. 浅谈SpringMVC执行过程

    通过深入分析Spring源码,我们知道Spring框架包括大致六大模块, 如Web模块,数据库访问技术模块,面向切面模块,基础设施模块,核心容器模块和模块, 其中,在Spring框架的Web模块中,又 ...