linux 编程笔记 2
1.使用create建立文件:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h> int main() {
int res = creat("./file.txt", );
if (res == -) {
perror("Create File Error!");
} else {
printf("Create OK!\n");
}
return ;
}

2.从输入到输出:
// 从stdin到sdtout #include <stdlib.h>
#include <stdio.h> int
main (int argc, char *argv[])
{
int c; while ((c = getchar()) != EOF) {
putchar(c);
} return ;
}
3.简单实现who命令:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <utmp.h>
#include <unistd.h>
#include <time.h> #define SHOWHOST void show_info(struct utmp* user_record);
void login_time(long *num_time); void show_info(struct utmp* user_record) { if (user_record->ut_type != USER_PROCESS) {
return ;
} printf("-------------------\n"); printf("User Name is: %10s\n", user_record->ut_name);
printf("User CMD is: %10s\n", user_record->ut_line);
login_time(&(user_record->ut_time)); #ifdef SHOWHOST
printf("User Host is: %s\n", user_record->ut_host);
#endif
printf("\n");
} void login_time(long *num_time) {
char *timestr;
timestr = ctime(num_time);
printf("User login time is: %30s\n", timestr);
} int main() { struct utmp current_record;
int record_len = sizeof(current_record);
int user_info_fd = -; // user info in the utmp if ((user_info_fd = open(UTMP_FILE, O_RDONLY)) == -) {
perror("read file error!");
exit();
} while (record_len == (read(user_info_fd, ¤t_record, record_len))) {
show_info(¤t_record);
} close(user_info_fd); return ;
}

4.简单实现cp命令:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h> #define BUFFERSIZE 4096
#define FILEMODE 0700 // 111 000 000 User all per void opps(char *file, char* argv); void opps(char *file, char* argv) {
fprintf(stderr, "Error : %s", file);
perror(argv);
exit();
} int main(int argc, char* argv[]) {
int in_fd = -;
int out_fd = -; int n_chars = -;
char buf[BUFFERSIZE]; if (argc != ) {
fprintf(stderr, "Usage: cp source dest\n");
exit();
} if ((in_fd = open(argv[], O_RDONLY)) == -) {
opps("Open First File Error", argv[]);
} if ((out_fd = creat(argv[], FILEMODE)) == -) {
opps("Creat Second File Error", argv[]);
} while ((n_chars = read(in_fd, buf, BUFFERSIZE)) > ) {
if (n_chars != write(out_fd, buf, n_chars)) {
opps("Write Error!", argv[]);
}
} if (- == n_chars) {
opps("Read Error!", argv[]);
} if (close(in_fd) == - || close(out_fd)) {
opps("Close Error!", " ");
} return ;
}

5.使用不同缓冲区的cp实验:
使用python得到5M多的一个文件
#!/usr/bin/env python
#-*- coding:utf-8 -*- fd = open("./data", "w+"); for i in xrange(500000):
fd.writelines("hello world!") fd.close()
分别使用1 4026 20000做为buf的cp实验:

使用缓冲区的利弊:
利:
1. 提高磁盘I/O效率
2. 优化磁盘的写操作
利弊:
如果不及时写入磁盘,会导致数据丢失。
可以使用sync 将缓冲区数据写入磁盘 通过
man sync 来查看详细说明
6.一个进程多次打开一个文件:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h> int main() {
int read_fd = open("./data", O_RDONLY);
int write_fd = open("./data", O_WRONLY);
int read_again_fd = open("./data", O_RDWR); char buf[];
read(read_fd, buf, );
buf[strlen(buf) - ] = '\0';
puts(buf);
close(read_fd); char str[] = "testing 123...";
write(write_fd, str, strlen(str));
close(write_fd); buf[] = '\0';
read(read_again_fd, buf, );
buf[strlen(buf) - ] = '\0';
puts(buf);
close(read_again_fd); return ;
}
每次都从最开始读取。
linux 编程笔记 2的更多相关文章
- Linux 编程笔记(三)
上一章节对文件的基本属性做了一个笔记,续上次笔记对Linux文件的属性和属性组做一笔记 我安装的是虚拟机操作系统的版本还KaliLinux但是系统启动速度拖延,所以刚开始还是配置Centos 1.Li ...
- Linux 编程笔记(四)
一.用户和用户组管理 添加新的用户账户使用useradd 格式useradd 选项 用户名 1.创建一个用户tian 其中 -d -m参数用来为登陆,登录名产生一个主目录 /usr/tian(其 ...
- linux 编程笔记1 crusher for linux
1.反显示字符crusher #include <stdio.h> int main (int argc, char *argv[]) { printf("\033[7m mor ...
- storysnail的Linux串口编程笔记
storysnail的Linux串口编程笔记 作者 He YiJun – storysnail<at>gmail.com 团队 ls 版权 转载请保留本声明! 本文档包含的原创代码根据Ge ...
- Linux网络编程笔记(修订版)
我的网络编程笔记, 因为最近又要做Linux下的网络编程,故重新修订, 其中一些内容参考了文末的链接及文章 1. 基本概念 2. 基本接口 2.1. 打开一个socket 2.2. 将 ...
- 笔记整理--Linux编程
linux c编程open() read() write()函数的使用方法及实例 | 奶牛博客 - Google Chrome (2013/8/31 17:56:10) 今天把文件IO操作的一些东东整 ...
- Linux内核笔记--深入理解文件描述符
内核版本:linux-2.6.11 文件描述符(file descriptor)在Linux编程里随处可见,设备读写.网络通信.进程通信,fd可谓是关键中的关键. 深入理解可以增加我们使用它的信心. ...
- Linux 学习笔记
Linux学习笔记 请切换web视图查看,表格比较大,方法:视图>>web板式视图 博客园不能粘贴图片吗 http://wenku.baidu.com/view/bda1c3067fd53 ...
- storysnail的Windows串口编程笔记
storysnail的Windows串口编程笔记 作者 He YiJun – storysnail<at>gmail.com 团队 ls 版权 转载请保留本声明! 本文档包含的原创代码根据 ...
随机推荐
- Nginx正则表达式之匹配操作符详解
nginx可以在配置文件中对某些内置变量进行判断,从而实现某些功能.例如:防止rewrite.盗链.对静态资源设置缓存以及浏览器限制等等.由于nginx配置中有if指令,但是没有对应else指令,所以 ...
- 使用Sed抽取MySQL安装文档的目录及行号
sed -nr -e '/^2.|^shell/=' -e '/^2.|^shell/p' INSTALL-SOURCE |awk '{if (NR%2==1) x=$1; else printf ...
- Spring 学习十四 Spring security安全
Spring security: 我用过的安全机制: oauth2, filter, secured方法保护 9.2 保护web请求: 9.2.1 代理Servlet过滤器: Delegat ...
- JavaScript设计模式--桥梁模式--引入
1.使用情况 (1)事件的监控 #1,利用页面的button来选择宠物的例子(思路) button.addEvent(element,"click",getPetByBame); ...
- SpringMvc之参数绑定注解详解之一
引言: 前段时间项目中用到了REST风格来开发程序,但是当用POST.PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没有加 任何注解),查看了提交方式为application/ ...
- UpdatePanel无刷新
使用UpdatePanel实现无刷新效果 1. <asp:ScriptManager ID="scriptManger1" ruant="server"& ...
- WPF Visibility属性用法
WPF Visibility属性用法 Visible 元素在窗体中正常显示 Collaspsed 元素不显示,也不占用空间 Hidden 元素不显示,但是任然为它保留空间
- poi包的几行基本的设置单元格样式
——杂言:写过很多遍这段代码,今天姑且记录一下,便于翻阅. jar:poi-3.7.jar 注意:第8行应该调用cellStyle.setFillForeGroundColor(HSSFColor.G ...
- OpenGL — GLFW — 颜色
OpenGL - GLFW - 颜色 参考教程:https://learnopengl-cn.readthedocs.io/zh/latest/02%20Lighting/01%20Colors/ 既 ...
- 32、Differential Gene Expression using RNA-Seq (Workflow)
转载: https://github.com/twbattaglia/RNAseq-workflow Introduction RNAseq is becoming the one of the mo ...