一.打开/创建文件:open()函数
int open(const char *pathname, intflags); /* 比较常用*/
int open(const char *pathname, intflags, mode_tmode);
//包含的头文件:
#include <sys/types.h>//这里提供类型pid_t和size_t的定义
#include <sys/stat.h>
#include <fcntl.h>
返回值:
成功,返回句柄,我们后面对于文件的读写,关闭等都通过句柄来操作。
失败,返回-1
参数说明:
pathname:文件的路径名,如果只写文件名,就默认当前目录,如果在文件名加上路径,就按照绝对路径来
打开文件。
flags:表示打开文件后用的操作。
O_RDONLY:只读模式
O_WRONLY:只写模式
O_RDWR:可读可写
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fd;
fd = open("./file1",O_RDWR);
printf("fd = %d\n",fd);
return 0;
}
若存在file1文件时,文件描述符的返回值
若不存在file1文件时,文件描述符的返回值
在文件有以上三种打开模式后,还能在后面添加几种模式:
O_APPEND 表示追加,如果原来文件里面有内容,则这次写入会写在文件的最末尾。
O_CREAT 表示如果指定文件不存在,则创建这个文件
O_EXCL 表示如果要创建的文件已存在,则出错,同时返回-1,并且修改errno 的值。
O_TRUNC 表示截断,如果文件存在,并且以只写、读写方式打开,则将其长度截断为0。
示例:O_CREAT:如果指定文件不存在,则创建这个文件
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fd;
fd = open("./file1",O_RDWR);
if(fd == -1){
printf("open file failed\n");
fd = open("./file1",O_RDWR|O_CREAT,0600);
if(fd > 0){
printf("create file success\n");
}
}
return 0;
}
示例: O_EXCL:如果同时指定了OCREAT,而文件已经存在,则返回-1。
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(){
int fd;
fd = open("./file1",O_RDWR|O_CREAT|O_EXCL,0600);
if(fd == -1){
printf("file Cunzai\n");
}
return 0;
}
示例:O_APPEND:如果原来文件里面有内容,则这次写入会写在文件的最末尾。
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main(){
int fd;
char *buf = "Hello World";
fd = open("./file1",O_RDWR|O_APPEND);//每次写时,都加到文件的尾端
int n_write = write(fd,buf,strlen(buf));
if(n_write != -1){
printf("write %d byte to file\n",n_write);
}
close(fd);
return 0;
}
示例: O_TRUNC:去打开文件时,如果原文件中有内容,则把原文件中的内容清零,再写入新文件。
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main(){
int fd;
char *buf = "Hello World";
fd = open("./file1",O_RDWR|O_TRUNC);// 如果原文件中有内容,则把原文件中的内容清零,再写入新文件
int n_write = write(fd,buf,strlen(buf));
if(n_write != -1){
printf("write %d byte to file\n",n_write);
}
close(fd);
return 0;
}
二.关闭文件操作:close()函数
int close(int fd);
//包含的头文件
#include <unistd.h>
功能就是简单的关闭文件
三.文件写入操作:write()函数
ssize_twrite (int fd,const void * buf, size_t count);
//包含的头文件
#include <unistd.h>
fd: 文件描述符
*buf: 写入的数据的首地址
count: 写入数据个数
返回值:如果顺利write()会返回实际写入的字节数(len)。当有错误发生时则返回-1,错误代码存入errno中
编程示例:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(){
int fd;
char *buf = "Hello World";
fd = open("./file1",O_RDWR);
if(fd == -1){
printf("open file1 fauled\n");
fd = open("./file1",O_RDWR|O_CREAT,0600);
if(fd > 0){
printf("create file1 success!\n");
}
}
write(fd,buf,strlen(buf));
close(fd);
return 0;
}
四.文件读取操作:read()函数
ssize_tread(int fd,void * buf, size_tcount);
//包含的头文件
#include <unistd.h>
fd: 文件描述符
*buf: 读入数据的首地址
count: 读入数据的个数
返回值:成功返回读取的字节数,出错返回-1并设置errno,如果在调read之前已到达文件末尾,则这次read
返回0
编程示例:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int fd;
char *buf = "Hello World!";
fd = open("./file1",O_RDWR);
if(fd == -1){
printf("open file1 failed\n");
fd = open("./file1",O_RDWR|O_CREAT,0600);
if(fd > 0){
printf("create file1 success\n");
}
}
printf("open success:fd = %d\n",fd);
int n_write = write(fd,buf,strlen(buf));
if(n_write != -1){
printf("write %d byte to file\n",n_write);
}
close(fd);
fd = open("./file1",O_RDWR);
char *readBuf;
readBuf = (char *)malloc(sizeof(char)*n_write+1);
int n_read = read(fd,readBuf,n_write);
printf("read %d,context:%s\n",n_read,readBuf);
close(fd);
return 0;
}
五.创建文件:creat()函数
int creat(const char *pathname, mode_t mode);
//包含的头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
参数说明:
const char *pathname 指要创建的文件路径(包括文件路径和文件名)
mode_t mode 指要创建文件的权限
S_IRWXU 可读可写可执行
S_IRUSR 可读
S_IWUSR 可写
S_IXUSR 可执行
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
int fd;
char *buf = "test";
fd = creat("/home/CLC/file1",S_IRWXU);
return 0;
}
六.文件光标移动操作:lseek()函数
off_t lseek(int fd, off_t offset, int whence);光标的偏移量
//包含的头文件
#include <sys/types.h>
#include <unistd.h>
fd : 文件描述符
Offset :偏移量
Whence :
SEEK_SET: 参数offset即为新的读写位置
SEEK_CUR: 以目前的读写位置往后增加offset个偏移量
SEEK_END: 将读写位置指向文件尾后再增加offset个位移量,当whence值为SEEK_CUR或SEEK_END时,
参数offset允许负值的出现。
返回值: 文件读写距离文件开头的字节大小,出错返回 -1
编程示例:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int fd;
char *buf = "Hello World!";
fd = open("./file1",O_RDWR);
if(fd == -1){
printf("open file1 failed\n");
fd = open("./file1",O_RDWR|O_CREAT,0600);
if(fd > 0){
printf("create file1 success\n");
}
}
printf("open success:fd = %d\n",fd);
int n_write = write(fd,buf,strlen(buf));
if(n_write != -1){
printf("write %d byte to file\n",n_write);
}
// close(fd);
// fd = open("./file1",O_RDWR);
char *readBuf;
readBuf = (char *)malloc(sizeof(char)*n_write+1);
lseek(fd,0,SEEK_SET);
int n_read = read(fd,readBuf,n_write);
printf("read %d,context:%s\n",n_read,readBuf);
close(fd);
return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int fd;
char *buf = "Hello World!";
fd = open("./file1",O_RDWR);
int filesize = lseek(fd,0,SEEK_END);
printf("filesize is %d\n",filesize);
close(fd);
return 0;
}