open
(1) 返回值
成功:文件描述符
失败:-1
(2) 打开方式
O_RDONLY 只读
O_WRONLY 只写
O_RDWR 读写
1.打开和创建一个文件
#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 shibai!\n");
fd=open("./file1",O_RDONLY|O_CREAT,0600);
if(fd>0){
printf("carete success!\n");
}
}
return 0;
}
0600=ABCD
A表示十进制
B表示用户
C表示组用户
D其他用户
x:执行 ->1
w:写入 ->2
r:读取 ->4
2.文件写入操作
#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="chenhailong!";
fd=open("./file1",O_RDWR);
if(fd==-1){
printf("open file1 shibai!\n");
fd=open("./file1",O_RDONLY|O_CREAT,0600);
write(fd,buf,strlen(buf));
close(fd);
if(fd>0){
printf("carete success!\n");
}
}
write(fd,buf,strlen(buf));
close(fd);
return 0;
}
文件读取操作需要移动光标
SEEK_SET:使光标移动到开始的位置
SEEK_CUR:使光标移动到当前的位置
SEEK_END: 使光标移动到末尾的位置
3.文件读取操作
#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="chenhailong!";
fd=open("./file1",O_RDWR);
if(fd==-1){
printf("open file1 shibai!\n");
fd=open("./file1",O_RDONLY|O_CREAT,0600);
write(fd,buf,strlen(buf));
close(fd);
if(fd>0){
printf("carete success!\n");
}
}
int n_write=write(fd,buf,strlen(buf));
if(n_write!=-1){
printf("write success!");
}
lseek(fd,0,SEEK_SET);
char *readBuf;
readBuf=(char *)malloc(sizeof(char )*n_write+1);
int n_read=read(fd,readBuf,n_write);
printf("%d number,%s\n",n_read,readBuf);
close(fd);
return 0;
}
在这里插入代码片
O_CREAT: 创建文件
O_EXCL: 同时和创建文件使用,如果文件存在会出错
O_APPEND: 每次写时都加到文件的尾端
O_TRUNC: 清空,如果文件有内容则清空
3.O_EXCL的用法,如果文件存在将会返回-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 yicunzai!\n");
}
return 0;
}
4.创建一个文件
宏表示 数字
S_IRUSR 4 可读
S_IWUSR 2 可写
S_IXUSR 1 可执行
S_IRWXU 7 可读、可写、可执行
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fd;
fd=creat("./file22",S_IRWXU);
if(fd>0){
printf("create success!\n");
}
return 0;
}
~
5.从文件另插一行
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
int main()
{
int fd;
int n_write;
char *buf="chenhailong!";
fd=open("./file1",O_RDWR|O_APPEND);
n_write=write(fd,buf,strlen(buf));
if(n_write==-1){
printf("printf no success!\n");
}
return 0;
}
6.用linux系统自带的文件描述符进行写入读取操作
STDIN_FILENO(0):标准输入
STDOUT_FILENO(1):标准输出
STDERR_FILENO(2):标准错误
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
char *buf;
buf=(char *)malloc(128);
read(0,buf,5);
write(1,buf,strlen(buf));
printf("\n");
return 0;
}
7.文件复制
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
/*srcv表示文件的个数,srcb接收3个需要操作的地址(1.复制指令,2.原文件,3。目标文件)*/
int main(int srcv,char **srcb)
{
int fdSrc;
int readSrc;
int fdDes;
int size;
char *buf=NULL;
if(srcv!=3){
printf("wrong!exit!");
exit(-1);
}
fdSrc=open(srcb[1],O_RDWR);
size=lseek(fdSrc,0,SEEK_END);
buf=(char *)malloc(sizeof(char)*size+8);
lseek(fdSrc,0,SEEK_SET);
int n_read=read(fdSrc,buf,size);
fdDes=open(srcb[2],O_RDWR|O_CREAT|O_TRUNC,0600);
int n_write=write(fdDes,buf,strlen(buf));
close(fdSrc);
close(fdDes);
return 0;
}
8.文件配置的修改
#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 arvg,char **arvc)
{
int fd;
char *buf;
int size;
if(arvg!=2){
printf("wrong!exit!\n");
exit(-1);
}
fd=open(arvc[1],O_RDWR);
size=lseek(fd,0,SEEK_END);
lseek(fd,0,SEEK_SET);
buf=(char *)malloc(sizeof(char)*size+8);
int n_read=read(fd,buf,size);
char *p=strstr(buf,"LENG=");
p=p+strlen("LENG=");
*p='3';
lseek(fd,0,SEEK_SET);
int n_write=write(fd,buf,strlen(buf));
close(fd);
return 0;
}
9.写整数到文件
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
int main(int avw,char **ave)
{
int fd;
int a=100;
int b;
fd=open(ave[1],O_RDWR);
int n_write=write(fd,&a,sizeof(int));
lseek(fd,0, SEEK_SET);
int n_read=read(fd,&b,sizeof(int));
printf("read=%d\n",b);
close(fd);
return 0;
}
10.写结构体到文件
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
struct text{
int a;
char b;
};
int main(int avw,char **ave)
{
int fd;
struct text a={1,'a'};
struct text b;
fd=open(ave[1],O_RDWR);
int n_write=write(fd,&a,sizeof(struct text));
lseek(fd,0, SEEK_SET);
int n_read=read(fd,&b,sizeof(struct text));
printf("read=%d %c\n",b.a,b.b);
close(fd);
return 0;
}