1.文件编程概述
api open
read/write
lseek
close
2.文件打开和创建
#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)
int creat(const char *pathname,mode_t mode)
pathname:要打开的文件名
flags: O_RDONLY只读打开 O_WRONLY只写打开 O_RDWR可读可写打开
下列常数是可以选择的:
O_CREAT若文件不存在则创建它,在使用此选项时,需要同时说明第三个参数mode
O_EXCL如果同时指定了O_CREAT,而文件已经存在,则出错
O_APPEND每次写时都加到文件的末端,不写会覆盖
O_TRUNC 属性去打开文件时,如果这个文件中本来是有内容的,而且为只读或只写成功打开,则将其长度截短为0
Mode:一定是在flags中使用O_CREAT标志,mode记录待创建的文件的访问权限
open的返回值就是文件描述符
#include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<stdio.h> int main(){ int fd; 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("creat file1 success\n"); } } printf("fd = %d\n",fd); return 0; }
3.创建文件creat函数
int creat(const char *filename,mode_t mode)
filename:要创建的文件名
mode:创建模式
常见创建模式:
S_IRUSR 4 可读
S_IWUSR 2 可写
S_IXUSR 1 可执行
S_IRWXU 7 可读可写可执行
4.文件写入操作编程
#include<unistd.h>
ssize_t write(int fd,const void *buf,size_t count)
#include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<stdio.h> #include<unistd.h> #include<string.h> int main(){ int fd; char *buf = "linqinlaoshi hen shuai!"; fd = open("./file1",O_RDWR); if(fd == -1){ printf("open file1 failed"); fd = open("./file1",O_RDWR|O_CREAT,0600); if(fd > 0){ printf("creat file1 success"); } } printf("open sucess fd = %d\n",fd); write(fd,buf,strlen(buf)); close(fd); return 0; }
5.文件读取操作
#include<unistd.h>
ssize_t read(int fd,void *buf,size_t count)
#include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<stdio.h> #include<unistd.h> #include<string.h> #include<stdlib.h> int main(){ int fd; char *buf = "linqinlaoshi hen shuai!"; 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("creat file1 success"); } } printf("open sucess 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; }
6.文件光标移动操作
文件“光标”位置
将文件读写指针相对whence移动offset个字节
#include<sys/types.h>
#include<unistd.h>
off_t lseek(int fd,off_t offset,int whence)
SEEK_SET 指向文件的头
SEEK_GND 指向文件的尾
SEEK_END 当前位置
#include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<stdio.h> #include<unistd.h> #include<string.h> #include<stdlib.h> int main(){ int fd; char *buf = "linqinlaoshi hen shuai!"; fd = open("./file1",O_RDWR); if(fd == -1){ perror("open file1 failed\n"); printf("open file1 failed\n"); fd = open("./file1",O_RDWR|O_CREAT,0600); if(fd > 0){ printf("creat file1 success"); } } printf("open sucess 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); } lseek(fd,0,SEEK_SET); 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; }
标签:文件,file1,int,编程,write,fd,include,open From: https://www.cnblogs.com/Lynchteacher/p/17357697.html