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 版权 转载请保留本声明! 本文档包含的原创代码根据 ...
随机推荐
- pip3 更改安装源
经常在使用Python的时候需要安装各种模块,而pip是很强大的模块安装工具,但是由于国外官方pypi经常被墙,导致不可用,所以我们最好是将自己使用的pip源更换一下,这样就能解决被墙导致的装不上库的 ...
- Poj 3903 Stock Exchange(LIS)
一.Description The world financial crisis is quite a subject. Some people are more relaxed while othe ...
- Spring MVC配置详解(3)
一.Spring MVC环境搭建:(Spring 2.5.6 + Hibernate 3.2.0) 1. jar包引入 Spring 2.5.6:spring.jar.spring-webmvc.ja ...
- 问题:JsonConvert;结果:JSON详解
JSON详解 JSON的全称是”JavaScript Object Notation”,意思是JavaScript对象表示法,它是一种基于文本,独立于语言的轻量级数据交换格式.XML也是一种数据交换格 ...
- .NET牛人养成计划
六大喜讯:(1)对于小型平板等授权免费(2)编译平台Rosly开源,ASP.NET全系平台开源(ASP.NET,Web API):ASP.NET跨平台,Mono,让ASP.NET运行在Linux和Un ...
- 每天一道算法题(4)——O(1)时间内删除链表节点
1.思路 假设链表......---A--B--C--D....,要删除B.一般的做法是遍历链表并记录前驱节点,修改指针,时间为O(n).删除节点的实质为更改后驱指针指向.这里,复制C的内容至B(此时 ...
- centos7 安装mysql 5.7多实例
一. Mysql多实例即一台服务器上运行多个Mysql服务进程 ,开启不同的服务端口,通过不同的socket 监听不同的服务端口来提供各自的服务. 二. Mysql多例有以下几个特点: 1. 有效利 ...
- SpringSecurity03 基于内存验证
1 需求 现有一个编写好的系统,需要实现用户登录验证即可,同时根据用户的权限来限制用户可以访问的接口 2 编写SpringSecurity配置类 继承 WebSecurityConfigurerAda ...
- Entity Framework Code-First(2):What is Code-First?
What is Code-First?: Entity Framework introduced Code-First approach from Entity Framework 4.1. Code ...
- adb devices unauthorized解决方法
有时候使用adb连接手机时,即使打开了usb调试,手机添加了信任,仍然出现unauthorized的提示 解决办法如下: 先上两张stack overflow上面的图片: 很多人可能看不懂.翻一下大概 ...