1.linux系统文件编程
在linux里面,系统会提供一些API 专属只在linux里面运行
比如 打开/创建 open/creat
读写 write/read
光标定位 lseek
关闭 close
1.打开/创建
在linux通过man手册可以查询相关的头文件及函数定义
open两种用法 成功的返回值是一个文件描述符 小的非负整数 失败时返回-1
int open(const char *pathname, int flags);
int open(const char *pathname, int flags,int mode);
pathname要打开的文件路径和名称
flags访问模式,宏定义和含义如下:
O_RDONLY 1 只读打开
O_WRONLY 2 只写打开
O_RDWR 4 读写打开
还可选择以下模式与以上3种基本模式相与 (|):
O_CREAT 0x0100 创建一个文件并打开
O_TRUNC 0x0200 打开一个已存在的文件并将文件长度设置为0 清空,其他属性保持
O_APPEND 0x0800 追加打开文件 在已有的内容后面添加
O_TEXT 0x4000 打开文本文件翻译CR-LF控制字符
O_BINARY 0x8000 打开二进制字符,不作CR-LF译
mode 该参数仅在flags=O_CREAT方式下使用,访问权限其取值如下:
S_IFMT 0xF000 文件类型掩码
S_IFDIR 0x4000 目录
S_IFIFO 0x1000 FIFO 专用
S_IFCHR 0x2000 字符专用
S_IFBLK 0x3000 块专用
S_IFREG 0x8000 只为0x0000
S_IREAD 0x0100 可读
S_IWRITE 0x0080 可写
S_IEXEC 0x0040 可执行
例如
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
int fd;
fd=open("./file1",O_RDWR);
printf("%d\n",fd);
return 0;
}
当前路径下没有文件file1 所以打开失败
此时除了在当前路径手动创建一个file1(touch file1)文件外 还可以用指令创建文件
改写open("./file1",O_RDWR| O_CREAT,0600); 此时在执行就会创建文件 并增加权限
0600就是文件file1的权限可读可写不可执行
当热也可以直接用creat创建文件
2.读写操作
#include <unistd.h> 头问件
ssize_t read(int fd, void *buf, size_t count);
ssize_t write(int fd, const void *buf, size_t count);
fd:文件描述符 buf:缓冲区 count:大小
buf是个指针类型的数据都行 不一定是字符串也可以是整数
write:将缓冲区buf内存里的数据写count个字节大小写到fd对应的文件里面
read:从fd指向的文件读取count个字节放到buf里面
写操作
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main()
{
int fd;
char *buf="hello world";
fd=open("./file1",O_RDWR);
write(fd,buf,strlen(buf));
printf("%s\n",buf);
close(fd);
return 0;
}
读写操作
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int fd;
char *buf="hello world";
fd=open("./file1",O_RDWR);
int n_write=write(fd,buf,strlen(buf));
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("%d\n",n_write);
printf("%d\n",n_read);
printf("%s\n",buf);
printf("%s\n",readbuf);
close(fd);
return 0;
}
因为光标的问题写完关闭 重新打开读取
3.光标移动
写完之后 光标会定位到最后一个字 这是如果读的话 是从光标的位置开始读的 所以光标需要重新移动到头位置
offset:偏移值
whence的参数选择
SEEK_SET:指向头
SEEK_END:指向尾
SEEK_CUR:当前位置
上述代码改进
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int fd;
char *buf="hello world";
fd=open("./file1",O_RDWR);
int n_write=write(fd,buf,strlen(buf));
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\n",n_write);
printf("%d\n",n_read);
printf("%s\n",buf);
printf("%s\n",readbuf);
close(fd);
return 0;
}
运行结果与上述一样
11个字节 也可以让光标偏移 使用lseek(fd,-11,SEEK_CUR); 运行结果一样
也可以使用光标求文件大小
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int fd;
char *buf="hello world";
fd=open("./file1",O_RDWR);
int seek=lseek(fd,0,SEEK_END);
printf("%d\n",seek);
close(fd);
return 0;
}
4.文件编程补充
linux系统有默认的标准输入输出文件描述符
0:标准输入
1:标准输出
2:标准错误
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<unistd.h>
int main()
{
int fd; //文件描述符
char *readbuf; //缓存区
readbuf=(char *)malloc(128);
memset(readbuf,'\0',128);
int n_read = read(0,readbuf,100); //读入100个字节存入缓存区readbuf
//不能用strlen因为刚开始空间为0 文件描述符为0 是标准输入也是键盘输入
int n_write = write(1,readbuf,strlen(readbuf)); //将缓存区的数据 以标准输出1的形式输出
printf("n_read =%d\n",n_read);
printf("n_write=%d\n",n_write); //字符数量
return 0;
}