不带缓冲区的I/O操作
1、open函数需要包含的头文件,以及函数的定义:
#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);
pathname: 文件路径字符串。
打开方式(flags):
O_RDWR(可读可写)
O_RDONLY(只读)
O_WRONLY(只写)
前面三个使用是互斥的。
可以用‘|’与O_CREAT使用,表示打开文件,如果没有则创建。
权限(mood)只有在创建文件时生效
返回值:-1表示失败。
例1:
#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 failed\n");
fd=open("./file1",O_RDWR|O_CREAT,0600);
if(fd>0){
printf("create file1 success\n");
}
}
return 0;
}
2、write函数头文件及函数定义:
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
参数fd为打开的文件流。
buf:针类型,指定的内存。
count:写入的字节数。
返回值:正常则返回字节数,返回1则表示有权限被禁止。
3、read函数头文件及函数定义:
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
从fd中读取到buf,读取count个字节。
返回值:正常返回读到的字节数,返回-1表示错误。
4、lseek数头文件及函数定义:
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);
offset:位移
whence:位置
参数类型有:SEEK_SET文件开头。
SEEK_CUR当前位置。
SEEK_END文件尾部。
返回值:成功返回距离开头多少字节,失败返回-1.
例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 fd;
char *buf="ss hen shuai";
fd=open("./file1",O_RDWR);
if(fd==-1)
{
printf("open file1 failed\n");
fd=open("./file1",O_RDWR|O_CREAT,0600);
if(fd>0){
printf("create file1 succes\n");
}
}
printf("open success fd=%d",fd);
int n_write = write(fd,buf,strlen(buf));
if(n_write!=1)
{
printf("write %d to file1 \n",n_write);
}
// close(fd);
// fd=open("./file1",O_RDWR);
char *readbuf;
readbuf =(char *)malloc(sizeof(char)*n_write+1);
lseek(fd,0,SEEK_SET);
int n_read=read(fd,readbuf,n_write);
printf("read %d,context:%s\n",n_read,readbuf);
close(fd);
return 0;
}
但是对文件的修改会覆盖文件里面的内容,可以在open的时候加入O_APPEND来追加到后面,或者用O_TRUNC清除原有内容。
5、实现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 fdes;
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+8);
int n_read=read(fdsrc,readbuf,size);
fdes=open(argv[2],O_RDWR|O_TRUNC|O_CREAT,0600);//清除原本内容
int n_write = write(fdes,readbuf,strlen(readbuf));
close(fdsrc);
close(fdes);
return 0;
}
文件配置
1、修改文件
#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 fdes;
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+8);
int n_read=read(fdsrc,readbuf,size);
char *p=strstr(readbuf,"LENG=");//返回找到串开头的位置
if(p==NULL)
{
printf("not found\n");
exit(-1);
}
p=p+strlen("LENG=");
*p='5';
lseek(fdsrc,0,SEEK_SET);
int n_write = write(fdsrc,readbuf,strlen(readbuf));
close(fdsrc);
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"
struct Test
{
int a;
char c;
};
int main()
{
int fd;
struct Test data1 ={100,'a'};
struct Test data2;
fd=open("./file1",O_RDWR);
int n_write = write(fd,&data1,sizeof(struct Test));
lseek(fd,0,SEEK_SET);
int n_read=read(fd,&data2,sizeof(struct Test));
printf("read %d,%c",data2.a,data2.c);
close(fd);
return 0;
}