linux系统编程--文件操作学习笔记
1. 在linux环境下学习C语言系统编程能够深入理解操作系统的理论知识
2. Linux下的系统编程是指程序员使用系统调用或C语言本身所携带的库函数来设计和编写具有某一特定功能的程序。
3. Linux系统是以文件为基础而设计的。
4. Linux所包含的文件类型:
普通文件;
目录文件;
字符特殊文件;
块特殊文件;
FIFO(用于进程的通信,也称为命名管道)
套接字
符号连接
5. 文件的访问权限控制:
【1】 在Shell下可以通过命令chmod来改变文件的访问权限。
【2】 通过chmod/fchmod函数可以对文件访问权限进行修改。
【3】 函数原型:
#include <sys/type.h>
#include <sys/stat.h>
int chmod(const char *path,mode_t mode);
//第一个参数为文件名
int fchmod(int fildes,mode_t mode);
//第一个参数为文件描述符
6. 文件的输入输出:
文件的创建,打开和关闭
【1】 open函数原型
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char* pathname,int flags);
int open(const char* pathname,int flags,mode_t mode);
//第一个参数为要打开或创建的含路径的文件名,flags表示文件的打开方式。Mode用于设置新文件的权限。
【2】 creat函数原型
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int creat(const char* pathname,mode_t mode);
//如果pathname只想的文件不存在,则创建一个新文件。如果存在,原文件会被新文件覆盖。
//creat作用与open(const char *pathname,(O_CREAT|O_WRONLY|O_TRUNC);
【3】 close函数原型
#include <unistd.h>
int close(int fd);
//fd表示要关闭的文件的文件描述符,它是从open或creat函数得到的。
//close函数调用时不能保证数据能全部写回硬盘。
文件的读写:
【1】 read函数原型
#include <unistd.h>
ssize_t read(int fd,void* buf,size_t count);
//从文件描述符fd所指向的文件中读取count个字节的数据到buf所指向的缓存当中。count为0则不读数据,read返回0.有错误发生返回-1,正常返回实际读到的字节数。
【2】write函数原型
#include <unistd.h>
ssize_t write(int fd,const void* buf,size_t count);
//将buf所指向的缓冲区中的count个字节数据写入到由文件描述符fd所指示的文件中。调用成功,write()返回写入的字节数。文件
//read和write函数都会引起文件读写指针的移动。
文件读写指针的移动:
lseek函数原型
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fildes,off_t offset,int whence);
//参数fildes为已打开的文件描述符,参数offset为根据参数whence来移动读写位置的位移数。Whence有三种取值:
SEEK_SET 文件开始处
SEEK_CUR 文件指针的当前位置
SEEK_END 文件结尾处,offset允许去负数
将文件读写指针移动到文件开头
lseek(int fildes,0,SEEK_SET);
将文件读写指针移动到文件结尾
lseek(int fildes,0,SEEK_END);
获取文件读写指针的当前位置
lseek(int fildes,0,SEEK_CUR);
有些设备(设备文件)如tty设备不能使用lseek。
Dup,dup2,fcntl,ioctl系统调用
【1】 dup和dup2函数原型
#include <unistd.h>
int dup(int oldfd);
int dup2(int oldfd,int newfd);
//dup用于复制参数oldfd所指的文件描述符。
//dup2用参数newfd指定新文件描述符的数值。
【2】fcntl函数原型
#include <unistd.h>
#include <fcntl.h>
int fcntl(int fd,int cmd);
int fcntl(int fd,int cmd,long arg);
int fcntl(int fd,int cmd,struct flock *lock);
【3】ioctl函数原型
#include <sys/ioctl.h>
int ioctl(int fd,int request,...);
//用于控制特殊设备文件的属性。fd必须是一个已经打开的文件描述符。第三个参数一般为char *argp,随第二个参数request不同而不同。Request决定了参数argp是传递数据还是获取数据。
7. 文件的属性操作
获取文件属性
【1]stat/fstat/lstat函数原型
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int stat(const char* file_name,struct stat* buf);
//获取由参数file_name指定的文件名的状态信息,保存在buf中
int fstat(int fildes,struct stat* buf);
//通过文件描述符来指定文件
int lstat(const char* file_name,struct stat *buf);
//对于符号链接文件,lstat返回符号链接文件本身的状态信息,stat返回符号链接所指向的文件状态信息
struct stat
{
dev_t st_dev;
ino_t st_ino;
mode_t st_mode;
nlink_t st_nlink;
uid_t st_uid;
gid_t st_gid;
dev_t st_rdev;
off_t st_size;
blksize_t st_blksize;
blkcnt_t st_blocks;
time_t st_atime;
time_t st_mtime;
time_t st_ctime;
}
设置文件属性
【1】hmod和fchmod
【2】chown/fchown/lchown函数原型
#include <sys/types.h>
#include <unistd.h>
int chown(const char *path,uid_t owner,gid_t group);
int fchown(int fd,uid_t owner,gid_t group_;
int lchown(const char* path,uid_t owner,gid_t group)'
//chown会把参数path指定的文件所有者id变更为参数owner代表的用户id,而将文件所有者的组变更为参数group组id。fchown和lchown类似,不过它们是以文件描述符为参数的。
【3】truncate/ftruncate函数原型
#include <unistd.h>
#include <sys/types.h>
int truncate(const char *path,off_t length);
int ftruncate(int fd,off_t length);
用于改变指定文件的大小,如果原文件的大小比参数大,超过的部分会被删除。如果原来的文件大小比参数length小,则文件将会被扩展,扩展部分以0扩充。同时文件大小的改变会引起st_mtime和st_ctime的改变。
【4】utime函数原型
#include <sys/types.h>
#include <utime.h>
int utime(const char* filename,struct utimbuf *buf);
#include <sys/time.h>
int utimes(char *filename,struct timeval *typ);
struct utimbuf
{
time_t actiome;//access time
time_t modtime;//modification time
};
//用于改变任何文件的st_mtime域和st_ctime域,即存取时间和修改时间。
【5】umask函数原型
#include <sys/types.h>
#include <sys/stat.h>
mode_t umask(mode_t mask);
//在进程创建一个新文件或目录时,如调用open函数创建一个新文件,新文件的实际存取权限安是mode和umask按照(mode&~umask)运算以后的结果。Umask函数用来修改进程的umask。
文件的移动和删除
【1】rename函数原型
#include <stdio.h>
int rename(const char* oldpath,const char* newpath);
【2】unlink函数和remove函数
#include <unistd.h>
int unlink(const char* pathname);
int remove(const char* pathname);
//unlink系统调用从文件系统中删除一个文件,如果文件的链接数为0且没有进程打开这个文件,则文件被删除且其占用的磁盘空间被释放。如果文件的链接数虽然为0,但是有进程打开了这个文件,则文件知道所有打开该文件的进程都结束了以后才删除该文件。
目录的创建和删除
【1】 mkdir函数原型
#include <sys/stat.h>
#include <sys/types.h>
int mkdir(const char* pathname,mode_t mode);
【2】rmdir函数原型
#include <unistd.h>
int rmdir(const char* pathname);
获取当前目录
#include <unistd.h>
char *getcwd(char buf,size_t size);
char *get_current_dir_name(void);
char *getwd(char *buf);
设置工作目录
#include <unistd.h>
int chdir(const char* path);
int fchdir(int fd);
//chdir将当前工作目录改为参数path指定的目录。Fchdir用来将当前工作目录改为由参数fd指定的目录。
获取目录信息
【1】opendir
#include<sys/types.h>
#include <dirent.h>
DIR opendir(const char* name);
【2】readdir
#include <sys/types.h>
#include <dirent.h>
struct dirent readdir(DIR *dir);
struct dirent
{
long d_ino;
off_t d_off;
unsigned short d_reclen;//length of the d_name
char d_name [NAME_MAX+1];//file name (null-terminated)
}
【3】closedir
#include <sys/types.h>
#include <dirent.h>
int closedir(DIR *dir);
posted on 2012-07-18 13:48 edward1992 阅读(275) 评论(0) 收藏 举报