文件操作
系统调用
write
//函数定义
#include <unistd.h>
size_t write(int fildes, const void *buf, size_t nbytes);
//示例程序
#include <unistd.h>
#include <stdlib.h>
int main()
{
if ((write(1, “Here is some data\n”, 18)) != 18)
write(2, “A write error has occurred on file descriptor 1\n”,46);
exit(0);
}
read
//函数定义
#include <unistd.h>
size_t read(int fildes, void *buf, size_t nbytes);
//示例程序
#include <unistd.h>
#include <stdlib.h>
int main()
{
char buffer[128];
int nread;
nread = read(0, buffer, 128);//打开文件后,0代表标准输入,1代表标准输出,2代表错误输出
if (nread == -1)
write(2, “A read error has occurred\n”, 26);
if ((write(1,buffer,nread)) != nread)
write(2, “A write error has occurred\n”,27);
exit(0);
}
重定向,输入重定向,管道功能
$ echo hello there | simple_read
# echo hello there 会将字符串 "hello there" 输出到终端上。
# | 是管道符,它将前面输出的内容作为后面命令的输入。
# simple_read 是一个命令行程序,它会从标准输入中读取一行文本,并将其打印到标准输出上。
# 将字符串 "hello there" 通过管道传递给 simple_read,simple_read 会将其读取并打印到终端上。
hello there
$ simple_read < draft1.txt #将文件" draft1.txt "的内容显示在终端
Files
In this chapter we will be looking at files and directories and how to manipulate
them. We will learn how to create files, o$
open
//函数定义
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
int open(const char *path, int oflags);
int open(const char *path, int oflags, mode_t mode);
open (“myfile”, O_CREAT, S_IRUSR|S_IXOTH);
close
//函数定义
#include <unistd.h>
int close(int fildes);
//应用示例:文件复制的程序
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
int main()
{
char c;
int in, out;
in = open(“file.in”, O_RDONLY);
out = open(“file.out”, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
while(read(in,&c,1) == 1)
write(out,&c,1);
exit(0);
}
lseek
//函数定义:确定文件的读写位置
#include <unistd.h>
#include <sys/types.h>
off_t lseek(int fildes, off_t offset, int whence);
❑ SEEK_SET: offset is an absolute position
❑ SEEK_CUR: offset is relative to the current position
❑ SEEK_END: offset is relative to the end of the file
fstat, stat, and lstat
//函数定义:通过文件名获取文件的索引结点,放在结构里
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
int fstat(int fildes, struct stat *buf);
int stat(const char *path, struct stat *buf);
int lstat(const char *path, struct stat *buf);
dup
//函数定义:复制文件描述符, 以实现多个进程共享同一个文件
#include <unistd.h>
int dup(int fildes);
int dup2(int fildes, int fildes2);
fopen(库函数)
//函数定义:返回一个指向FILE类型结构体的指针。如果文件打开成功,则返回指向FILE类型结构体的指针;否则返回NULL。
#include <stdio.h>
FILE *fopen(const char *filename, const char *mode);
❑ “r” or “rb”: Open for reading only
❑ “w” or “wb”: Open for writing, truncate to zero length
❑ “a” or “ab”: Open for writing, append to end of file
❑ “r+” or “rb+” or “r+b”: Open for update (reading and writing)
❑ “w+” or “wb+” or “w+b”: Open for update, truncate to zero length
❑ “a+” or “ab+” or “a+b”: Open for update, append to end of file
The b indicates that the file is a binary file rather than a text file.
fread(通过文件流指针读写)
#include <stdio.h>
size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
/*
ptr:指向存储读取数据的缓冲区的指针。
size:每个数据项的字节数。
nitems:要读取的数据项的数量。
stream:指向FILE类型结构体的指针,代表要读取的文件流。
库函数的读写是记录式读写
*/
fwrite(通过文件流指针读写)
#include <stdio.h>
size_t fwrite (const void *ptr, size_t size, size_t nitems, FILE *stream);
fclose
#include <stdio.h>
int fclose(FILE *stream);
fflush(刷新)
#include <stdio.h>
int fflush(FILE *stream);
fseek
//设置读写的指针
#include <stdio.h>
int fseek(FILE *stream, long int offset, int whence);
//offset:偏移位置
//whence:对位置的解释,是绝对位置还是相对位置还是从末尾数的位置...
opendir
//打开一个目录
#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);
readdir
//读取目录的记录
#include <sys/types.h>
#include <dirent.h>
struct dirent *readdir(DIR *dirp);