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, &current_record, record_len))) {
show_info(&current_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的更多相关文章

  1. Linux 编程笔记(三)

    上一章节对文件的基本属性做了一个笔记,续上次笔记对Linux文件的属性和属性组做一笔记 我安装的是虚拟机操作系统的版本还KaliLinux但是系统启动速度拖延,所以刚开始还是配置Centos 1.Li ...

  2. Linux 编程笔记(四)

    一.用户和用户组管理 添加新的用户账户使用useradd 格式useradd   选项  用户名 1.创建一个用户tian 其中 -d -m参数用来为登陆,登录名产生一个主目录 /usr/tian(其 ...

  3. linux 编程笔记1 crusher for linux

    1.反显示字符crusher #include <stdio.h> int main (int argc, char *argv[]) { printf("\033[7m mor ...

  4. storysnail的Linux串口编程笔记

    storysnail的Linux串口编程笔记 作者 He YiJun – storysnail<at>gmail.com 团队 ls 版权 转载请保留本声明! 本文档包含的原创代码根据Ge ...

  5. Linux网络编程笔记(修订版)

    我的网络编程笔记, 因为最近又要做Linux下的网络编程,故重新修订, 其中一些内容参考了文末的链接及文章 1.   基本概念 2.   基本接口 2.1.   打开一个socket 2.2.   将 ...

  6. 笔记整理--Linux编程

    linux c编程open() read() write()函数的使用方法及实例 | 奶牛博客 - Google Chrome (2013/8/31 17:56:10) 今天把文件IO操作的一些东东整 ...

  7. Linux内核笔记--深入理解文件描述符

    内核版本:linux-2.6.11 文件描述符(file descriptor)在Linux编程里随处可见,设备读写.网络通信.进程通信,fd可谓是关键中的关键. 深入理解可以增加我们使用它的信心. ...

  8. Linux 学习笔记

    Linux学习笔记 请切换web视图查看,表格比较大,方法:视图>>web板式视图 博客园不能粘贴图片吗 http://wenku.baidu.com/view/bda1c3067fd53 ...

  9. storysnail的Windows串口编程笔记

    storysnail的Windows串口编程笔记 作者 He YiJun – storysnail<at>gmail.com 团队 ls 版权 转载请保留本声明! 本文档包含的原创代码根据 ...

随机推荐

  1. 用Fiddler2来监听HTTP(记:用skydrive sdk访问时,出错后用Fidder抓包分析)

    最近在写一个关于如何上传文件到skydrive的demo, 用REST上传失败. 安装Telerik的Fiddler后, 可以监听http或者https通信, 然后可以在软件中看到返回的json数据或 ...

  2. ansible一键部署LAMP

    一.实现ansible跟节点间无密码访问,不会配置的请看 文章 . 二.创建目录 $ mkdir -p playbooks/{files,templates}   三.创建php测试文件index.p ...

  3. linux下将编译错误输出到一个文本文件

    linux下将编译错误输出到一个文本文件 command > filename 把把标准输出重定向到一个新文件中 command > > filename 把把标准输出重定向到一个文 ...

  4. Unity添加自定义快捷键——UGUI快捷键

    在Editor下监听按键有以下几种方式: 自定义菜单栏功能: using UnityEngine; using UnityEditor; public static class MyMenuComma ...

  5. 看下面代码输出结果Java

    编译错误 在调用子类构造器之前,会先调用父类构造器,当子类构造器中没有使用"super(参数或无参数)"指定调用父类构造器时,是默认调用父类的无参构造器,如果父类中包含有参构造器, ...

  6. Apple Swift中文入门教程【转发】

    1   简介 今天凌晨Apple刚刚发布了Swift编程语言,本文从其发布的书籍<The Swift Programming Language>中摘录和提取而成.希望对各位的iOS& ...

  7. rxjs入门6之合并数据流

    一 concat,merge,zip,combineLatest等合并类操作符 以上操作符在版本6中已经只存在静态方法,不能在pipe中使用. import {concat,merge,zip,com ...

  8. Ubuntu使用技巧

    命令 获取系统安装包的编译源码及脚本 apt-get source package 查询端口被占用的进程 lsof -i:端口号 配置 配置阿里源 # mv /etc/apt/source.list ...

  9. 审美赛_(java)实现

    问题描述 <审美的历程>课上有n位学生,帅老师展示了m幅画,其中有些是梵高的作品,另外的都出自五岁小朋友之手.老师请同学们分辨哪些画的作者是梵高,但是老师自己并没有答案,因为这些画看上去都 ...

  10. D. Beautiful Array

    题目:http://codeforces.com/contest/1155/problem/D 给你n,x,一个n个数的序列,你可以选择一段区间,区间的数都乘以x,然后求出最大字段和 竟然是很简单的d ...