在没有IDE的时候,记住一些常用的库函数的函数名、参数、基本用法及注意事项是很有必要的。
参照Linux_C_HS.chm的目录,我大致将常用的函数分为一下几类:
1. 内存及字符串控制及操作
2. 字符串转换
3. 字符测试
4. 文件操作
5. 时间日期
6. 常用数学函数
7. 文件内容操作
8. 文件权限控制
9. 进程操作
10. 线程操作
11. Socket操作
12. 信号处理
13. 数据结构及算法
这次主要总结的是上面黑色部分,关于文件操作的函数。
系统调用归类
* * 函数名 用法 备注 * *1. int open(const char *pathname, int flags); open and possibly create a file or device flags 必须包含O_RDONLY, O_WRONLY, or O_RDWR中的任何一个 * *2. int open(const char *pathname, int flags, mode_t mode); UP mode只是在flags中包含O_CREAT时才有效 * *3. int fsync(int fd); synchronize a file's in-core state with storage device 写完数据close前必须先同步,以防意外 * *4. off_t lseek(int fd, off_t offset, int whence); 定位文件位置 第三个参数可以为SEEK_SET SEEK_CUR SEEK_END * *5. ssize_t read(int fildes, void *buf, size_t nbyte); UP 文件位置会随读取的字节数移动 * *6. ssize_t write(int fildes, const void *buf, size_t nbyte); UP UP * *7. int close(int fd); UP UP * *8. void *mmap(void *addr, size_t length, int prot, int flags, 内存映射 先用fstat得到文件大小,然后使用该函数将文件内容映射到内存中,然后就可以 int fd, off_t offset); 直接调用字符串函数操作。 * *9. int munmap(void *addr, size_t length); 释放内存 UP * *10. int ftruncate(int fd, off_t length); truncate a file to a specified length 在新建文件时,如果用到内存映射,那么就需要先调用该函数给文件预置一个大小,否则会出错 * *11. int fstat(int fd, struct stat *buf); 获得文件的基本信息 具体参数见man,可以查到文件的大小、修改日期等 * |