1.open
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, const char *argv[])
{
//定义文件描述符
int fd;
//更改权限掩码的值
//umask(0);
if((fd=open("./bb.txt", O_WRONLY|O_CREAT, 0666)) == -1)
{
perror("open error");
return -1;
}
return 0;
}
#include<stdio.h>
#include<errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, const char *argv[])
{
//定义文件描述符
int fd;
//打开文件
if((fd = open("./bb.txt", O_RDWR|O_CREAT|O_EXCL, 0664)) == -1)
{
//判断错误原因
if(errno == EEXIST) //如果文件存在,则会将错误码置EEXIST
{
printf("文件已存在,无需重复创建\n");
fd = open("./bb.txt", O_RDWR);
}else
{
perror("open error");
return -1;
}
}
return 0;
}
2.close
#include<stdio.h>
#include<errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<unistd.h>
int main(int argc, const char *argv[])
{
//定义文件描述符
int fd;
//打开文件
if((fd = open("./bb.txt", O_RDWR|O_CREAT|O_EXCL, 0664)) == -1)
{
//判断错误原因
if(errno == EEXIST) //如果文件存在,则会将错误码置EEXIST
{
printf("文件已存在,无需重复创建\n");
fd = open("./bb.txt", O_RDWR);
}else
{
perror("open error");
return -1;
}
}
//关闭文件
if(close(fd) == -1)
{
printf("文件关闭失败\n");
}else
{
printf("文件关闭成功\n");
}
return 0;
}
3.write和read
#include<myhead.h>
int main(int argc, const char *argv[])
{
//定义文件描述符
int fd;
//打开文件,以只写的形式
if((fd = open("./05test.txt", O_WRONLY|O_CREAT|O_TRUNC, 0664)) == -1)
{
perror("open error");
return -1;
}
//输出文件描述符
printf("fd = %d\n", fd); //3
//定义容器存放数据
char buf[128] = "good good study day day up";
//将数据写入文件中去
write(fd, buf, sizeof(buf));
//关闭文件
close(fd);
//重新以只读的方式打开文件
if((fd = open("./05test.txt", O_RDONLY)) == -1)
{
perror("open error");
return -1;
}
char buf1[16];
int ret = read(fd, buf1, sizeof(buf1)); //从文件中读取16字节的数据
//printf("buf1 = %s\n", buf1);
write(1, buf1, ret); //将数据写入标准输出文件描述符中
//关闭文件
close(fd);
return 0;
}
#include<myhead.h>
int main(int argc, const char *argv[])
{
//判断传入文件个数
if(argc != 3)
{
printf("input file error\n");
printf("usage:./a.out srcfile dstfile\n");
return -1;
}
//定义两个文件描述符
int src_fd,dst_fd;
//以只读的形式打开源文件
if((src_fd=open(argv[1], O_RDONLY)) == -1)
{
perror("open error");
return -1;
}
//以只写的形式打开目标文件
if((dst_fd=open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0664)) == -1)
{
perror("open error");
return -1;
}
//定义容器搬运数据
char buf[128] = "";
int ret; //记录每次搬运的字符个数
//ret = read(src_fd, buf, sizeof(buf));
while((ret=read(src_fd, buf, sizeof(buf))) > 0)
{
write(dst_fd, buf, ret);
}
//关闭文件
close(src_fd);
close(dst_fd);
printf("拷贝成功\n");
return 0;
}
4.lseek
#include<myhead.h>
int main(int argc, const char *argv[])
{
//定义文件描述符
int fd;
//打开文件
if((fd = open("./01test.c", O_RDONLY)) == -1)
{
perror("open error");
return -1;
}
printf("文件大小为:%ld\n", lseek(fd, 0, SEEK_END)); //?
//关闭文件
close(fd);
return 0;
}
5.dup
#include<myhead.h>
int main(int argc, const char *argv[])
{
//定义文件描述符
int fd1,fd2;
//使用只读的方式打开fd1
if((fd1=open("./08test.txt", O_RDONLY)) == -1)
{
perror("open error");
return -1;
}
printf("fd1 = %d\n", fd1); //3
//调用文件描述符拷贝函数
fd2 = dup(fd1);
printf("fd2 = %d\n", fd2); //4
//通过fd1将光标移动到第6个字节处
lseek(fd1, 6, SEEK_SET);
//关闭文件描述符
if(close(fd1) != 0)
{
perror("close fd1 error");
return -1;
}
//使用fd2从文件中读取数据,如果读取的数据是hello说明不共享光标
//如果读取的是world说明共享同一光标
char buf[5];
int ret = read(fd2, buf, sizeof(buf));
//将该数据写入标准输出中
write(1, buf, ret);
printf("\n");
if(close(fd2) != 0)
{
perror("close fd2 error");
return -1;
}
return 0;
}
6.dup2
#include<myhead.h>
int main(int argc, const char *argv[])
{
//定义两个文件描述符
int fd1,fd2;
//让两个文件分别打开不同的文件
if((fd1=open("./05test.txt", O_RDONLY)) == -1)
{
perror("open fd1 error");
return -1;
}
if((fd2=open("./08test.txt", O_RDWR)) == -1)
{
perror("open fd2 error");
return -1;
}
/*
printf("fd1 = %d\n", fd1); //3
printf("fd2 = %d\n", fd2); //4
//调用文件描述符拷贝函数
dup2(fd2, fd1); //操作结束后,fd1和fd2都指向fd2原本指向的文件
printf("fd1 = %d\n", fd1); //3
printf("fd2 = %d\n", fd2); //4
//将fd1偏移6字节
lseek(fd1, 6, SEEK_SET);
//通过fd2进行读取数据,如果读取的结果为hello则说明不共享光标
//如果读取的结果为world,则说明共享光标
char buf[5];
read(fd2, buf, sizeof(buf));
//将读取的数据展示到终端上
write(1, buf, sizeof(buf));
*/
//常用的操作
dup2(fd2, 1); //将stdout的文件描述符先关闭,并将将1号文件描述符号
//的功能更改成fd2,从此之后,所有对标准输出进行的操作,全部都是对文件的操作
//该文件也被称为日志文件
printf("你好呀\n");
printf("夕阳无限好、只是近黄昏\n");
//关闭文件
close(fd1);
close(fd2);
return 0;
}
7.stat
#include<myhead.h>
int main(int argc, const char *argv[])
{
struct stat sb; //文件状态的变量
//调用文件状态函数,获取文件状态
stat("./01test.c", &sb);
printf("inode:%ld\n", sb.st_ino); //文件的iNode号
printf("type:%#o\n", sb.st_mode&S_IFMT); //文件的类型
printf("mode:%#o\n", sb.st_mode&0777); //文件权限
return 0;
}