Linux文件操作,目录结构,文件模型,操作函数

文件操作
学习本章要达到的目标:
o1 .了解Linux 文件系统的目录结构;
o2 .理解Linux 的文件模型;
o3 .掌握Linux 关于文件操作的函数的功能;
o4 .熟练应用Linux系统中文件操作的函数进行程序设计。
11.1 Linux 的文件系统
o 几乎所有的操作系统所使用的文件系统在面向
用户的结构上都是大同小异的,都采用树或森
林的结构
o  在UNIX 系列的系统中是根目录 “/ ”
o Linux没有盘符的概念,文件路径的起始为
根目录 “/ ” 。通常在根目录下包含如下目
录:
n boot  启动相关的程序和配置;
n bin 常用的Linux 命令,这些命令通常为可
执行文件或这些文件的链接;
n sbin  通常为根用户准备的命令;
n lib 系统常用库;
n usr 用户安装的文件、库、开发库等;
n root  根用户的用户文件;
n home 普通用户的用户文件;
n etc 系统或程序的配置文件;
n var 系统中服务器数据、日志等;
n proc  系统状态信息;
n dev  系统设备;
n mnt 、media 其他分区的挂载点(如
Windows 分区、光盘或软盘等);
n tmp  临时文件;
n lost+found 磁盘孤立扇区。
o 对于应用程序的开发,我们需要把文件看成
是 “ 流 ” 。
o 在操作文件的过程中,我们需要控制一个假
想中的指针。该指针指向文件的一个位置,
这个位置便是当前操作文件的位置。这个指
针会随着我们的操作而自动移动
11.2  文件操作相关函数
o  文件控制
o  目录操作
o  文件流读写控制
o  文件读写操作
11.2.1  文件控制
o  文件重命名
n #include <stdio.h>
n int rename(const char *oldpath,
const char *newpath);
o  删除文件
n #include <stdio.h>
n int remove(const char *pathname);
o  修改文件的所有者
n #include <sys/types.h>
n #include <unistd.h>
n int chown(const char *path, uid_t
owner, gid_t group);
o  修改文件的访问权限。
n #include <sys/types.h>
n #include <sys/stat.h>
n int chmod(const char *path, mode_t
mode);
o  此外还可以使一下宏的或组合:
n S_IRUSR / S_IREAD 文件所有者具有读权限,
n S_IWUSR / S_IWRITE 文件所有者具有写权限,
n S_IXUSR / S_IEXEC 文件所有者具有执行权限,
n S_IRGRP 用户组具有读权限,
n S_IWGRP 用户组具有写权限,
n S_IXGRP 用户组具有执行权限,
n S_IROTH 其他所有用户具有读权限,
n S_IWOTH 其他所有用户具有写权限,
n S_IXOTH 其他所有用户具有执行权限。
11.2.2  目录操作
o  获取当前工作目录
n #include <unistd.h>
n char *getcwd(char *buf, size_t
size);
o  改变当前工作目录
n #include <unistd.h>
n char *getcwd(const char *path);
o  打开目录
n #include <sys/types.h>
n #include <dirent.h>
n DIR *opendir(const char *name);
o  关闭已打开的目录
n #include <sys/types.h>
n #include <dirent.h>
n int closedir(DIR *dir);
o 读取目录中一个文件的内容,并将目录流指
针后移
n #include <sys/types.h>
n #include <dirent.h>
n struct dirent *readdir(DIR *dir);
n struct dirent {
n ino_t d_ino;
n ff_t d_ff;
n signed short int d_reclen;
n unsigned char d_type;
n char d_name[256];
n };
o  获取指定目录流当前指针位置
n #include <sys/types.h>
n #include <dirent.h>
n off_t telldir(DIR *dir);
o  设置指定目录流的针位置
n #include <sys/types.h>
n #include <dirent.h>
n void seekdir(DIR *dir, off_t offset);
11.2.3  文件流读写控制
o  打开文件,获取文件流指针
n #include <stdio.h>
n FILE *fopen(const char *path, const
char *mode);
o  关闭已打开的文件
n #include <stdio.h>
n int fclose(FILE *stream);
o  获取文件流指针当前的读写位置
n #include <stdio.h>
n long ftell(FILE *stream);
o  设置文件流指针的读写位置
n #include <stdio.h>
n int fseek(FILE *stream, long offset,
int whence);
o 判断文件流指针的当前读写位置是否已达到
文件尾
n #include <stdio.h>
n int feof(FILE *stream);
o  从指定的文件流中读取一个字符
n #include <stdio.h>
n int fgetc(FILE *stream);
o  将单个字符写入指定的文件流中
n #include <stdio.h>
n int fputc(int c, FILE *stream);
o  从指定的文件流中读取一个字符串
n #include <stdio.h>
n char *fgets(char *s, int size, FILE
*stream);
o  将字符串写入指定的文件流中
n #include <stdio.h>
n int fputs(char *s, FILE *stream);
o  从指定文件流中读取一段数据
n #include <stdio.h>
n size_t fread(void *ptr, size_t size,
size_t nmenb, FILE *stream);
o  将指定的一段数据写入指定文件流
n #include <stdio.h>
n size_t fwrite(void *ptr, size_t size,
size_t nmenb, FILE *stream);
11.2.4  文件读写操作
o  打开指定文件,返回文件标识符
n #include <sys/types.h>
n #include <sys/stat.h>
n #include <fcntl.h>
n int open(const char *pathname, int
flags, mode_t mode);
n O_RDONLY 以只读的形式打开文件,
n O_WRONLY 以只写的形式打开文件,
n O_RDWR 以可读写的形式打开文件;
n O_CREAT 如果被打开文件不存在,则自动建立文件,
n O_EXCL 与O_CREAT 一起使用,若文件存在则报错;
n O_TRUNC 以可写方式打开时,清空文件内容。
o  创建新文件
n #include <sys/types.h>
n #include <sys/stat.h>
n #include <fcntl.h>
n int creat(const char *pathname,
mode_t mode);
o  创建临时内存文件
n #include <stdlib.h>
n int mktemp(char *template);
o  关闭文件
n #include <unistd.h>
n int close(int fd);
o  从指定的文件中读取数据
n #include <unistd.h>
n ssize_t read(int fd, void *buf, size_t
count);
o  将数据写入指定的文件中
n #include <unistd.h>
n ssize_t write(int fd, void *buf, size_t
count);
o  修改文件的读写位置
n #include <sys/types.h>
n #include <unistd.h>
n off_t lseek(int fd, off_t offset, int
whence);
o  创建无名管道
n #include <unistd.h>
n int pipe(int filedes[2]);
o  创建有名管道,该管道被看作一个文件
n #include <sys/types.h>
n #include <sys/stat.h>
n int mkfifo(const char *pathname,
mode_t mode);
11.3  应用实例训练
o  本章的实例训练,我们来完成一个 “ 小小辞
典 ”,它是一个电子辞典软件的简化版本。
我们主要借用该软件的开发来理解Linux 下
的文件操作。作为简化版本的电子辞典,我
们主要提供如下两个功能即可:查单词、添
加单词。
11.3.1  程序分析
o 对于文件中数据的组织,常常会分类两类,
一类是为计算机运行时提供数据的,这类文件常采用二进制形式行存储,该类文件人类
通过文本查看软件是无法阅读的,但因为其
存储格式同计算机内部操作的数据格式基本
一致,因此计算机程序读取起来比较方便;
另一种是为人阅读或修改而提供的,这类文
件常采用文本形式进行存储,这类文件虽然
可以通过文本察看软件进行阅读,但对于计
算机解读起来比较麻烦,常常需要转换。
o 选择了文本形式存储以后,那么使用流方式
比较方便,因此这里我们选择标准库函数中
提供的文件流操作的相关函数
o 软件的层次应该是这样。首先,我们需要提
供行读写的封装函数。其次,再次之上提供
词典操作函数。在最上层完成界面的编写。
11.3.2  程序编写
o /*  函数的定义 */
o void FileReadLine(FILE *file, char *buf);
o void FileWriteLine(FILE *file, char *buf);
o
o int DictFindWord(FILE *file, char *word, char
*exp);
o void DictAddWord(FILE *file, char *word, char
*exp);
o
o int DecodeCommand(char *cmdbuf, char
*wordbuf, char *expbuf, int bufsize);
o int GetCommandCatagory(char cmdcatach);
o void PrintHelpInfo();
11.3.3  编译与运行
o gcc dictionary.c -o dictionary
o ./dictionary
 

猜你喜欢

转载自blog.csdn.net/qq_38971487/article/details/91492753