文章目录
1.系统api和库函数关系
1.1内存结构
比如调用printf(“hello\n”)函数时,系统回调用系统函数write(1,“hello”,6);其中第一个参数表示标准输出stdout(–>1)。
2.文件IO
2.1open函数
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
- pathname文件名
- flags文件权限
- 必选项
- O_RDONLY 只读
- O_WRINLY 只写
- O_RDWR 读写
- 可选项
- O_APPEND 追加
- O_CREAT 创建文件
- mode文件权限为(mode&~unmsk)
- 返回值:返回最小的可用文件描述符。打开失败返回-1,设置errmo
2.2close函数
int close(int fd);
- 返回值:关闭成功返回0,失败返回-1,设置errno;
实现简单的touch指令
//实现我的mytouch
#include<stdi.h>
#include<sys.h>
#include<fcntl.h>
#include<unistd.h>
int main(int argc,char*argv[]){//argc参数个数,注意为1时代表没有参数(只有程序名字),argv[]是指参数,argv[1]指第一个参数,argv[2]指第二个参数。。。
if(argc!=2){
printf("./a.out filename\n");
return -1;
}
//打开文件,权限为O_RDNOLY;如果文件不存在就创建一个文件,文件的权限为666;
//返回打开文件的文件描述符
int fd=open(argv[1],O_RDONLY|O_CREAT,066);
//关闭文件
close(fd);
return 0;
}
2.3read()函数
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
/*
fd表示文件描述符,在linux中识别对应的文件
buf 缓存区 count表示缓冲区大小
返回值:
读取失败返回-1,设置errno;
读取到EOF,返回0;
其他,如果返回读取的大小
*/
2.4write()函数
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
/*
fd:文件描述符
buf:缓冲区,指针类型为void*
count:缓冲区大小,单位为比特位
返回值:
成功:返回写入的字节数
失败:返回-1,设置errno
什么都没做:返回0
*/
实现cat指令
#include <unistd.h>
#include<stdio.h>
#inlcude<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
void print(int fd){
int i,len;
char buf[10];
len=read(fd,buf,10);
while(len>0){
for(i=0;i<len;i++){
printf("%c",buf[i]);
}
len=read(fd,buf,10); //read()返回读取字节数。遇到EOF就停止。
}
}
void prin(){
char buf[1024];
while(1){
scanf("%s",buf);
puts(buf);
}
}
int main(int argc,char*argv[]){//argc参数个数,注意为1时代表没有参数(只有程序名字),argv[]是指参数,argv[1]指第一个参数,argv[2]指第二个参数。。。
int fd,i;
if(argc==1){//没有参数,跳转到prin函数
prin();
return 0;
}
for(i=1;i<argc-1;i++){//argc参数个数,注意argc为1时代表没有参数,只有程序的名字。
fd=open(argv[i],O_RDWR);//以可写可读的方式打开
if(fd==-1){
perror("Error");//
}//如果成功打开
else{
print(fd);
}
close(fd);
}
return 0;
}
2.5lseek函数
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);
/*
fd:文件描述符
offset:拓展大小
whence:
SEEK_SET:文件开始位置
SEE_CUR:位置
SEEK_END:文件结束位置
返回值:
成功:返回当前位置到开始位置的长度
失败:返回-1;设置errno
*/
作用:
- 移动文件读写位置
- 计算文件大小
- 拓展文件
//计算文件大小
#include <unistd.h>
#include<stdio.h>
#inlcude<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
int main(int argc,char*argv[]){
if(argc!=2){ //如果argc不为2,即参数个数不为1
printf("./a.out filename\n");
return -1;
}
int fd=open(argv[1],O_RDONLY);
//SEEK_END表示文件的末尾,lseek返回当前位置到开始位置的长度
int ret=lseek(fd,0,SEEK_END);
printf("file size if %d",ret);
close(fd);
return 0;
}
//拓展文件
int main(int argc,char*argv[]){
if(argc!=2){ //如果argc不为2,即参数个数不为1
printf("./a.out filename\n");
return -1;
}
int fd=open(argv[1],O_WDONLY|O_CREAT);
//拓展文件
int ret=lseek(fd,1024,SEEK_END);
//文件需要写入东西,否则无法保存
write(fd,"a",1);
close(fd);
return 0;
}
2.6阻塞和非诸塞(O_NONBLOOK)
read函数在读设备或读管道,或者读网络的时候,处于等待状态时就称为阻塞。
int main(int argc,char*argv[]){
int fd=open("/dev/tty",O_RDWR);
char buf[256];
while(1){
ret=read(fd,buf,sizeof(buf));
if(ret){
printf("buf is %s\n",buf);
}
}
printf("hello");
sleep(1);
close(fd);
return 0;
}
发生阻塞
设置为非阻塞
int main(int argc,char*argv[]){
int fd=open("/dev/tty",O_RDWR|O_NONBLOOK);
char buf[256];
while(1){
ret=read(fd,buf,sizeof(buf));
if(ret){
printf("buf is %s\n",buf);
}
}
printf("hello");
sleep(1);
close(fd);
return 0;
}
2.7fcntl函数设置非阻塞
int fcntl(int fd, int cmd, … /* arg */ );
fd:文件描述符
cmd:
int main(int argc,char*argv[]){
int fd=open("/dev/tty",O_RDWR);
//设置为非阻塞,决策函数
int flags=fcntl(fd,F_GETFL); //得到文件的flag
flags|=O_NONBLOOK; //flag|=0_NONBLOOK
fcntl(fd,F_SETFL,flags); //再次调用fcntl
char buf[256];
while(1){
ret=read(fd,buf,sizeof(buf));
if(ret){
printf("buf is %s\n",buf);
}
}
printf("hello");
sleep(1);
close(fd);
return 0;
}