Linux C 文件操作
来源:互联网 发布:淘宝做什么产品好 编辑:程序博客网 时间:2024/06/05 14:34
文件操作应该是Linux系统最常用的操作了,Linux把系统中的所有的设备都映射成了文件,所以操作设备,也就是在操作文件。
Linux上的文件操作个人理解可以分为两个部分,一个是C标准库的部分(stdio.c),还有一个就是Linux系统标准接口。
1.先来介绍标准库的部分吧,常用的函数就是打开文件fopen,写入文件fputc, fputs, fwrite,关闭文件fclose.
函数原型如下:
FILE * fopen( char *path, char * mode ) ; //mode分为r,w,a,分别对应读,写,和添加打开方式int fclose( FILE *stream );int fputc( int c, FILE *stream );int fputs( char *s, FILE *stream );size_t fwrite( void *ptr, size_t size, size_t nmemb, FILE *stream )//这个主要用来存储结构数组,ptr为结构数组头指针,size为结构大小,nmemb为数组大小
2. 接下来是系统接口:
int open( char *path, int flag ) ;//返回文件标识来给read,write用int close( int fd );size_t read( int fd, void *buff, size_t count );size_t write( int fd, void *buff, size_t count );
文件接口其实并不复杂,除了这些,还涉及到磁盘缓存的概念,因为为了效率原因,并不是每次操作都需要真实的去写硬盘,c标准库提供了fflush和Linux接口提供了sync操作来强制系统进行磁盘操作。
对于标准库而言,因为是针对所有的系统的比如Linux,Windows等等,所以底层实现需要系统支持的,比如现在这个文件操作,实际最终也是调用系统接口的。
所以这两个部分可以理解成C标准库中的文件操作是用第二个系统标准接口实现的,其实我们也可以自己实现的。
用图来表示可以如下:
接下来对比一下两个库的效率。
以写入50000行日志为例:
#include <stdio.h>#include <time.h>#include <fcntl.h>#include <stdlib.h>#include <string.h>int main_cstadard( int argc, char *argv[] ){ if ( argc != 3 ) { printf( "usage:\ngenlog 100 log.txt\n" ); return 1; } long len = atoi( argv[1] ); FILE * fd = fopen( argv[2], "w+" ); if ( fd != NULL ) { int i = 0; char buff[30]; struct tm *gmTime; time_t timeTPre = time( NULL ); for ( ; i < len; i++ ){ time_t timeT = time(NULL); gmTime = gmtime( &timeT ); sprintf( buff, "%4d-%02d-%02d %02d:%02d:%02d %d\n", gmTime->tm_year + 1900, gmTime->tm_mon + 1, gmTime->tm_mday, gmTime->tm_hour, gmTime->tm_min, gmTime->tm_sec, i ); fputs( buff, fd );}; fclose( fd ); printf( "Log %s is finished. \nCost time %d sec\n", argv[2], time(NULL) - timeTPre ); }}int main_linuxapi( int argc, char *argv[] ){ if ( argc != 3 ) { printf( "usage:\ngenlog 100 log.txt\n" ); return 1; } long len = atoi( argv[1] ); int fd = open( argv[2], O_RDWR | O_TRUNC | O_CREAT ); if ( fd != -1 ) { int i = 0; char buff[30]; struct tm *gmTime; time_t timeTPre = time( NULL ); for ( ; i < len; i++ ){ time_t timeT = time(NULL); gmTime = gmtime( &timeT ); sprintf( buff, "%4d-%02d-%02d %02d:%02d:%02d %d\n", gmTime->tm_year + 1900, gmTime->tm_mon + 1, gmTime->tm_mday, gmTime->tm_hour, gmTime->tm_min, gmTime->tm_sec, i ); write( fd, buff, strlen( buff ) );}; close( fd ); printf( "Log %s is finished. \nCost time %d sec\n", argv[2], time(NULL) - timeTPre ); }}
输出结果,标准库花了1秒,直接调用API花了16秒,这个应该就是C标准库缓冲区比较大,做了些优化,带来的好处。
[chenndao@localhost log]$ gcc -o genlogc genlog.c
[chenndao@localhost log]$ ./genlogc 50001 50001.txt
Log 50001.txt is finished.
Cost time 1 sec
[chenndao@localhost log]$ ./genlog 50001 50000.txt
Log 50000.txt is finished.
Cost time 15 sec
- linux c 文件操作
- Linux C 文件操作
- 【LINUX C】文件操作
- Linux C 文件操作
- Linux C 文件操作
- linux C文件操作相关
- Linux c 开发 - 文件操作
- linux c 文件操作相关
- linux-C编程-文件操作
- C Linux 文件操作总结
- linux c 文件操作汇总
- linux c文件操作函数
- linux c文件操作接口
- linux-C编程-文件操作
- [Linux C编程]文件操作
- [Linux C编程]文件操作
- Linux c文件操作总结
- [Linux C编程]文件操作
- c++单独编译和c++的多文件组织结构
- 什么是数据库
- Boost库学习随记四 pool内存池使用示例:pool、object_pool、singleton_pool、pool_alloc内存池
- thinkphp自定义标签
- linux版 myeclipse 下载地址
- Linux C 文件操作
- ORACLE LIST
- Linux-01-安装分区及入门
- ARP原理
- Python3.4字符串对齐/删除空格/合并/反转
- 什么是云计算
- graphviz 工具厉害
- 铁电存储器 - Ferroelectric Random Access Memory(FRAM)
- Android Performance(8) MAT(整理)