目录
一、文件打开与创建(open函数与creat函数)
1.open函数
1.作用
用来打开或者创建一个文件,还可以根据参数制定文件的属性和用户权限。
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);
1.参数pathname 是想要打开的文件路径名。
2.参数flags 选取模式必须且只能选取下面其中一个。
1.O_RDONLY 只读模式
2.O_WRONLY 只写模式
3.O_RDWR 可读可写模式
选用:用来和上面的必选项进行按位或起来作为flags参数。(常用1~4点)
1.O_APPEND 如果原来文件里面有内容,加到文件的尾端,不改变原有的内容。
2.O_CREAT 如果指定文件不存在,则创建这个文件。使用此选项时,需要同时说明 第三个参数mode,用其说明该新文件的存取许可权限。
3.O_EXCL 如果要创建的文件已存在,则出错,同时返回 -1,并且修改 errno 的值。
4.O_TRUNC 如果文件里有内容,而且以只写、读写方式打开,则将其长度截断为0。
5.O_NOCTTY 如果路径名指向终端设备,不要把这个设备用作控制终端。
6.O_NONBLOCK 如果路径名指向 FIFO/块文件/字符文件,则把文件的打开和后继 I/O设置为非阻塞模式。
3.参数mode 设置创建的文件的访问权限,一定是在flags中使用了O_CREAT标志。
数字 文件属性
1.S_IRUSR 4 可读
2.S_IWUSR 2 可写
3.S_IXUSR 1 可执行
4.S_IRWXU 7 可读、写、执行
4.返回值 操作成功:返回一个文件描述符(非负整数)。
操作失败,返回-1。
举例:
fd = open("./wlkq",O_RDWR );//在本路径(保存当前代码的路径)打开名为wlkq文件
fd = open("./wlkq",O_RDWR|O_CREAT,S_IRWXU);//在本路径(保存当前代码的路径)打开名为wlkq文件 不存在则创建 且权限是可读、写、执行
fd 是文件描述符
3.例子
打开一个文件,如果文件不存在则创建一个文件。
#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 faied\n");
fd = open("./file1",O_RDWR|O_CREAT,0600);//6是4+2 可写可读
if(fd > 0){
printf("create file1 success\n");
}
}
return 0;
}
运行结果:
在保存代码的路径下多了一个file文件。
2.creat函数
1.作用
创建一个文件,可以根据参数设置用户权限。
2.头文件及定义
代码:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int creat(const char *filename, mode_t mode);
1.参数filename 要创建的文件名(包含路径、缺省当路径)。
2.参数mode 设置创建的文件的访问权限。
数字 文件属性
1.S_IRUSR 4 可读
2.S_IWUSR 2 可写
3.S_IXUSR 1 可执行
4.S_IRWXU 7 可读、写、执行
3.返回值 成功:返回一个文件描述符(非负整数)。
失败:返回-1。
举例:
fd = creat("/home/ldz/linux/lie3",S_IRWXU);
3.例子
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
int fd;
fd = creat("/home/ldz/linux/lie3",S_IRWXU);
if(fd>0){
printf("ok\n");
}
if(fd == -1){
printf("no\n");
}
return 0;
}
运行结果:
在/home/ldz/linux下生成名为lie3的文件。
二、写入文件(write函数)
1.write函数
1.作用
将缓冲区buf里面的数据写最多count个字节到文件描述符fd的文件里。
2.头文件及定义
代码:
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
1.参数fd 是文件描述符。
2.参数buf 是一个无类型的指针,缓冲区。
3.参数count 是要写入文件指定的字节数。
4.返回值 操作成功:返回写入的字节数(0表示什么也没写入) 。
操作失败:返回-1。
举例:
write(fd,buf,strlen(buf));//strlen计算缓冲区buf大小
3.例子
#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 = "jia you";
fd = open("./file1",O_RDWR);
if(fd == -1){
printf("open file1 faied\n");
fd = open("./file1",O_RDWR|O_CREAT,0600);
if(fd > 0){
printf("create file1 success\n");
}
}
printf("open susceess:fd = %d\n",fd);
write(fd,buf,strlen(buf));
return 0;
}
运行结果:
file1文件里面写入了jia you。
光标:此时在这,先了解下面再讲。
三、关闭文件(close函数)
1.close函数
1.作用
关闭一个文件描述符,让它不再引用任何文件,并且可以重用。
2.头文件及定义
#include <unistd.h>
int close(int fd);
1.参数fd 是文件描述符。
3.例子
#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 = "jia you";
fd = open("./file1",O_RDWR);
if(fd == -1){
printf("open file1 faied\n");
fd = open("./file1",O_RDWR|O_CREAT,0600);
if(fd > 0){
printf("create file1 success\n");
}
}
printf("open susceess:fd = %d\n",fd);
write(fd,buf,strlen(buf));
close(fd);
return 0;
}
关闭了fd文件。
光标:关闭文件后,如果重新打开,光标会回到头部。
四、读取文件(read函数)
1.read函数
1.作用
在文件描述符fd的文件里读取最多count个字节,然后放到缓冲区buf。
2.头文件及定义
#include <unistd.h>
ssize_t read(int fd, void * buf, size_t nbytes);
1.参数fd 是文件描述符。
2.参数buf 是一个无类型的指针,缓冲区。
3.参数count 是读取文件指定的字节数。
4.返回值 操作成功:返回读取的字节数(0表示什么也没读到)。
操作失败:返回-1。
举例:
read(fd,readbuf,n_write);//readbuf是指针
3.例子
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main()
{
int fd;
char *buf = "jia you";
char *readbuf;
fd = open("./file1",O_RDWR);
if(fd == -1){
printf("open file1 faied\n");
fd = open("./file1",O_RDWR|O_CREAT,0600);
if(fd > 0){
printf("create file1 success\n");
}
}
printf("open susceess: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);//打开 让光标回到文件头部
readbuf = (char *)malloc(sizeof(char)*n_write + 1);//给readbuf分配空间
int n_read = read(fd,readbuf,n_write);//得到读取的字节数
printf("erad %d ,context:%s\n",n_read,readbuf);//输出字节数 与读取内容
close(fd);
return 0;
}
运行结果:
读取了jia you。
五、文件光标移动(lseek函数)
1.lseek函数
1.作用
将文件读写指针相对whence移动offset个字节。
2.头文件及定义
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);
1.参数fd 是文件描述符。
2.参数offset 是偏移值(正数往后偏,负数往前偏)。
3.参数whence 相对点的位置。
1.SEEK_SET 指向文件的头
2.SEEK_CUR 当前位置。
3.SEEK_END 指向文件的尾。
4.返回值 成功:返回偏移多少个字节。
失败:返回-1。
举例:
lseek(fd,0,SEEK_SET);//指向文件头,偏移值0,此时指向文件头
lseek(fd,0,SEEK_END);//指向文件尾,偏移值0,此时指向文件尾
lseek(fd,-a,SEEK_END);//指向文件尾,a为文件共多少个字节,负数往前偏,此时光标指向文件头
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 = "jia you";
char *readbuf;
fd = open("./file1",O_RDWR);
if(fd == -1){
printf("open file1 faied\n");
fd = open("./file1",O_RDWR|O_CREAT,0600);
if(fd > 0){
printf("create file1 success\n");
}
}
printf("open susceess: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);//打开 让光标回到文件头部
lseek(fd,0,SEEK_SET);//代替了之前的关开文件改变光标
readbuf = (char *)malloc(sizeof(char)*n_write + 1);
int n_read = read(fd,readbuf,n_write);
printf("erad %d ,context:%s\n",n_read,readbuf);
close(fd);
return 0;
}
运行结果:
六、实例
1.实现cp指令
1.实现cp指令之前先了解main函数,main函数有两种形式
1.函数没有参数,返回值为 int 类型
int main( void )
2.函数有参数,分别是 int 和 char**,返回值是 int 类型。
int main( int argc, char *argv[ ] )
//argc 传入参数的个数
//argv 存放参数的指针数组,数组中每一项都是存放着一个参数的地址
带有参数的代码:
2.cp指令代码及 注释!:
#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 argc,char **argv)
{
int fdSrc;
int fdDes;
char *readBuf = NULL;
if(argc != 3){//需要三个参数
printf("par arm error\n");
exit(-1);
}
fdSrc = open(argv[1],O_RDWR);//打开需复制的文件
int size = lseek(fdSrc,0,SEEK_END);//通过移动光标到尾部返回移动的大小,获得文件的大小
lseek(fdSrc,0,SEEK_SET);//光标移回头部
readBuf = (char *)malloc(sizeof(char)*size+1);//开辟文件大小的空间
int n_read = read(fdSrc,readBuf,size);//读取内容
if(n_read > 0){
printf("du qu ok\n");
}
fdDes =open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0600);//打开目标文件,没有则创建
int n_write = write(fdDes,readBuf,strlen(readBuf));//写入内容
if(n_write > 0){
printf("xie ru ok\n");
}
close(fdSrc);
close(fdDes);
return 0;
}
2.修改文件内容
代码及 注释!:
#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 argc,char **argv)
{
int fdSrc;
int fdDes;
char *readBuf = NULL;
if(argc != 2){
printf("par arm error\n");
exit(-1);
}
fdSrc = open(argv[1],O_RDWR);
int size = lseek(fdSrc,0,SEEK_END);
lseek(fdSrc,0,SEEK_SET);
readBuf = (char *)malloc(sizeof(char)*size+1);
int n_read = read(fdSrc,readBuf,size);
if(n_read > 0){
printf("du qu ok\n");
}
//===================重点代码
char *p = strstr(readBuf,"LENG");//在readBuf寻找LENG,并返回首次出现的地址
if(p == NULL){
printf("not\n");
exit(-1);
}
p = p+strlen("LENG=");//将指针移到需要修改的字符地址
*p = '5';//修改
//===================
lseek(fdSrc,0,SEEK_SET);
int n_write = write(fdSrc,readBuf,strlen(readBuf));
if(n_write > 0){
printf("xie ru ok\n");
}
close(fdSrc);
return 0;
}
~
修改后的文件内容(LENG=9 --> LENG=5)