Linux
系统编程1–基本文件操作(open、w/r、lseek、creat
)
文章目录
常用命令
Ctrl + Shift +
放大Ctrl -
缩小yy
粘贴,p
复制dd
删除 num ddu
撤销./
当前目录下的文件Ctrl Alt t
打开终端tab
自动补全mv
移动,mv *[]
剪切、mv a.c b.c
重命名set nu
显示行标
命令
pwd
显示当前文件夹路径sudo
管理员权限mkdir
建立文件夹cd
进入某个文件夹cd ..
退出某个文件夹ls
列出文件夹ls -a
列出所有文件夹cp a.c b.c
a b 拷贝cd
回到工作目录touch file1
创建新的空文件rm
删除ls -l
单列输出man 2 write
查看Linux中的指令帮助 /read
1、文件编程
- 打开:
open
- 读写:
write / read
- 光标定位:
lseek
- 关闭:
close
- 创造:
creat
1.1 open 打开
int open(const char *pathname, int flags);
int open(const char *pathname, int flags,mode_t mode);
返回文件描述符
fd
,非负代表成功,-1代表失败
- 打开成功:返回值不为 -1;
- 打开失败:返回值为 -1;
- 添加头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcnt1.h>
- flag: 三选一
O_RDONLY
:只读O_WRONLY
:只写打开O_RDWE
: 可读可写打开
- O_CREAT:文件不存在,这创建;
//demo2.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
int fd; //open函数返回的文件描述符 fd
fd = open("./file1",O_RDWR);
if(fd == -1) //返回值是 -1,表示失败!
{
printf("open file1 failed\n");
fd = open("./file1",O_RDWR|O_CREAT,0600);
//O_RDWE|O_CREAT 表示如果文件不存在,创建这个文件&加权限0600
//可读r 4 可写w 2 执行x 1 0600指的是 4+2
if(fd > 0)//成功,返回值大于0
{
printf("create file1 success!\n");
}
}
printf("fd = %d\n",fd);
return 0;
}
O_EXCL:文件存在,则报错;//用来判断文件是否存在;
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
int fd;
fd = open("./file",O_RDWR|O_CREAT|O_EXCL,0600); //不存在生成,存在就报错,可以判断文件是否存在
if(fd == -1)
{
printf("file exist!");
}
return 0;
}
O_APPEND:每次写的时候都加到文件的尾端;
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h> //write写入的头文件
#include <string.h> //strlen函数所需头文件
#include <stdlib.h>
int main()
{
int fd;
char *buf = "tp22 nb666";
fd = open("./file1",O_RDWR|OAPPEND);//每次写的时候都加到文件的尾端;
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);
return 0;
}
O_TRUNC:把原来所有的内容都干掉,将长度截断为0
1.2 write 写入
ssize_t write(int fd, const void *buf, size_t count);
写入成功:返回值 count; 写入失败:返回值为1;
- 中间的参数 ,buf不一定要是字符串,也可以是地址,(字符串也可以理解为地址)
const viod *buf
//也可以是地址
int data = 100;
int = n_write = write(fd, &data,sizeof(int));
- 添加头文件
#include<unistd.h>
将
count
个来自buf
里的数写入到fd
- 演示
//demo3.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h> //write写入的头文件
#include <string.h> //strlen函数所需头文件
int main()
{
int fd; //open函数返回的文件描述符 fd
char *buf = "tp666 nb666"; //字符串在编译器里面是 地址
fd = open("./file1",O_RDWR);
if(fd == -1) //返回值是 -1,表示失败!
{
printf("open file1 failed\n");
fd = open("./file1",O_RDWR|O_CREAT,0600);
//O_RDWE|O_CREAT 表示如果文件不存在,创建这个文件&加权限0600
//可读r 4 可写w 2 执行x 1 0600指的是 4+2
if(fd > 0)//成功,返回值大于0
{
printf("create file1 success!\n");
}
}
printf("open success :fd = %d\n",fd);//fd = 3
//写入
//ssize_t write(int fd,const void *buf,size_t count);
write(fd,buf,strlen(buf)); //写入文件,将buf里的数据写入到 file1 文件中
close(fd); //关闭文件
return 0;
}
1.3 read 读取
ssize_t read(int fd, void *buf, size_t count);
从
fd
指向的文件读取count
个字节的数据放在buf
里
- 读取成功:返回读取数;
- 读取失败:返回 -1;
- 添加头文件
#include<unistd.h>
-
先写入,后读取 的光标问题
- 光标移到头,再读(下一小节介绍)
- 重新打开(这一小节用这个)
-
演示
//demo4.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h> //write写入的头文件
#include <string.h> //strlen函数所需头文件
#include <stdlib.h> //malloc函数
int main()
{
int fd; //文件描述符 //open函数的返回值
char *buf = "tp666 nb666"; //字符串在编译器里面是 地址
fd = open("./file1",O_RDWR);
if(fd == -1) //返回值是 -1,表示失败!
{
printf("open file1 failed\n");
fd = open("./file1",O_RDWR|O_CREAT,0600);
//O_RDWE|O_CREAT 表示如果文件不存在,创建这个文件&加权限0600
if(fd > 0)//成功,返回值大于0
{
printf("create file1 success!\n");
}
}
printf("open success :fd = %d\n",fd);
//写入
//ssize_t write(int fd,const void *buf,size_t count);
int n_write = write(fd,buf,strlen(buf)); //写入文件,返回写入字节数
if(n_write != -1)
{
printf("write %d byte to file1\n",n_write);
}
//读取
//读取前,处理以下光标,重新打开
close(fd);//关闭
fd = open("./file1",O_RDWR);//重新打开,光标初始化
char *readBuf;
readBuf = (char *)malloc(sizeof(char)*n_write + 1);//防止断错误
//ssize_t read(int fd, void *buf, size_t count);
int n_read = read(fd, readBuf, n_write);
//从 fd 所指向的文件中,读取 n_write 个字节的数据,放在 readBuf 里面
printf("read %d,context:%s\n",n_read,readBuf);
close(fd); //关闭文件
return 0;
}
1.4 lseek
光标的移动
off_t lseek(int fd, off_t offset, int whence);
-
将文件读写指针相对 whence 移动 offset 个字节;
-
成功 返回 偏移值 offset;//针对文件头,偏移多少个字节;
//计算文件大小 int fd; char *buf = "tp222 nb666"; fd = open("./file1",O_RDWR); int filesize = lseek(fd, 0, SEEK_END); printf("file`s size is %d\n",filesize);
失败 返回 -1;
-
whence :固定点的一个位置;
- SEEK_SET : 文件的头
- SEEK_END: 文件的尾
- SEEK_CUR: 当前光标位置
-
offset : 对 whence 的一个偏移值
- 正数往前走;
- 复数往后走;
-
添加头文件
#include <sys/types.h>
#include <unistd.h>
- 演示
//demo5.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h> //write写入的头文件
#include <string.h> //strlen函数所需头文件
#include <stdlib.h> //malloc函数
int main()
{
int fd; //文件描述符 //open函数的返回值
char *buf = "tp666 nb666"; //字符串在编译器里面是 地址
fd = open("./file1",O_RDWR);
if(fd == -1) //返回值是 -1,表示失败!
{
printf("open file1 failed\n");
fd = open("./file1",O_RDWR|O_CREAT,0600);
//O_RDWE|O_CREAT 表示如果文件不存在,创建这个文件&加权限0600
if(fd > 0)//成功,返回值大于0
{
printf("create file1 success!\n");
}
}
printf("open success :fd = %d\n",fd);
//写入
//ssize_t write(int fd,const void *buf,size_t count);
int n_write = write(fd,buf,strlen(buf)); //写入文件,返回写入字节数
if(n_write != -1)
{
printf("write %d byte to file1\n",n_write);
}
//读取
//读取前,处理以下光标,重新打开
//close(fd);//关闭
//fd = open("./file1",O_RDWR);//重新打开,光标初始化
char *readBuf;
readBuf = (char *)malloc(sizeof(char)*n_write + 1);//防止断错误
//off_t lseek(int fd, off_t offset, int whence);//offset:正数往前移,负数往后移
lseek(fd,0,SEEK_SET);//针对文件头的偏移值是 0;
//lseek(fd,-1*n_write, SEEK_CUR);//也可以这样
//ssize_t read(int fd, void *buf, size_t count);
int n_read = read(fd, readBuf, n_write);
//从 fd 所指向的文件中,读取 n_write 个字节的数据,放在 readBuf 里面
printf("read %d,context:%s\n",n_read,readBuf);
close(fd); //关闭文件
return 0;
}
1.5 create 创造
int creat(const char *filename, mode_t mode)
- mode:创建模式
- S_IRUSR 4 可读
- S_IWUSR 2 可写
- S_IXUSR 1 可执行
- S_IRWXU 7 可读、写、执行
- 添加头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcnt1.h>
- 演示
#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 = "test";
// int creat(const char *pathname, mode_t mode);
fd = creat("/home/tp/桌面/program/file1",S_IRWXU); //可读、写、执行
return 0;
}