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 ;
}

每次都从最开始读取。

最新文章

  1. OpenCV从入门到放弃系列之——图像的基本操作
  2. SQL Server 数据库操作类
  3. Natural Language Processing Computational Linguistics
  4. Ubuntu下面配置问题
  5. (转) Special members
  6. grivid中切换按钮,两个按钮交替
  7. VMWare桥接、NAT和only-host三种模式
  8. Fisher Vector Encoding and Gaussian Mixture Model
  9. mysql导出指定字段或指定数据到文件中
  10. 爬虫_网页url设计
  11. warning: ISO C++ forbids converting a string constant to &#39;char*&#39; [-Wwrite-strings]
  12. [Android] Android RxBus 用法学习总结
  13. Winform调用百度地图接口简单示例
  14. webserver
  15. Frps 家庭服务器访问解决方案
  16. ubuntu apache ssl配置
  17. Oracle 解决【ORA-01704:字符串文字太长】
  18. Centos7 Zabbix3.2集群安装
  19. (转)MySQL高可用架构之MHA
  20. Python实例---简单购物车Demo

热门文章

  1. varnish安装和配置
  2. Android audioManager
  3. k8s 基础(4) k8s安装
  4. Android精品资源汇总,10个源码(持续更新)
  5. Selenium并行启动多个浏览器
  6. 【总结整理】JQuery基础学习---动画
  7. express搭建elasticsearch
  8. python笔记——均值、方差、中位数计算
  9. push和commit的区别
  10. 《鸟哥的Linux私房菜》读书笔记1