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 版权 转载请保留本声明! 本文档包含的原创代码根据 ...
随机推荐
- python中如何定义main方法
我们有时写的python模块需要自己测试, 简单方法就是定义main函数, 然后测试自己的模块接口. def main(): test_yourCode() if __name__ == & ...
- OpenStack安装后检查流程总结
安装后检查 1. 确保服务正常运行 首先查看服务的运行状态: #service xxx status 为防止对子服务有疏漏,可使用ps + grep 查看: # ps aux |grep xx 2. ...
- ArrayList,Vector, LinkedList的存储性能和特性
ArrayList和Vector都是使用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,它们都允许直接按序号索引元素,但是插入元素要涉及数组元素移动等内存操作,所以索引数据快而插入 ...
- java报表开发之报表总述
转自:https://blog.csdn.net/u011659172/article/details/40504271?utm_source=blogxgwz6
- centos7安装etcd
http://blog.csdn.net/dream_broken/article/details/52671344
- 菜鸟攻城狮2(JAVA开发环境)
1.JDK下载路径:www.oracle.com/technetwork/java/javase/downloads 2.安装案例:最后一步认证操作 win+R 或者 点击开始--〉运行 输入“cmd ...
- 31、NGS 常用分析软件
转载:http://www.zilhua.com/2081.html 参考资料:http://bioinfo.mc.vanderbilt.edu/NGS/software.htm 1. Mapping ...
- Python通过调用windows命令行处理sam文件
Python通过调用windows命令行处理sam文件 以samtools软件为例 一.下载或者索取得到windows版本的samtools软件,解压后如下: 进入文件内部,有如下几个文件: 二.将s ...
- Win10访问不了Samba网络共享的解决办法
一,首先安装SMB功能 1.打开"应用和功能",选择“程序和功能” 2.选择"开启或关闭windows功能" 3.选中下面红框中三个选项,点击确定,然后重启,就 ...
- SQLServer数据库权限设置--保障数据库安全
一.登陆界面引入 下图为SQL Server的登陆界面. 1)服务器名称:“.”代表本地计算机,选择下拉框,可以看见还有一个与本机机名相同的内容,也代表于本地服务器连接:要连接远程服务器的话,在此处填 ...