本文主要是介绍《UNIX环境高级编程》笔记--read函数,write函数,lseek函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.read函数
- #include <unistd.h>
- ssize_t read(int filedes, void* buff, size_t nbytes);
#include <unistd.h>
ssize_t read(int filedes, void* buff, size_t nbytes);
成功则返回实际读取的byte数,如果已经达到文件结尾则返回0,出错则返回-1. 2.write函数
- #include <unistd.h>
- ssize_t write(int filedes, void* buff, size_t nbytes);
#include <unistd.h>
ssize_t write(int filedes, void* buff, size_t nbytes);
成功则返回实际写入的byte数,出错则返回-1. 3.lseek函数
每个打开的文件都有一个关联的“当前偏移量”,用于记录从文件到当前当前位置的偏移字节数,lseek函数是设置这个当前偏移量
- #include <unistd.h>
- off_t lseek(int filedes, off_t offset, int whence);
#include <unistd.h>
off_t lseek(int filedes, off_t offset, int whence);
成功则返回新的文件偏移量,失败则返回-1. - off_t currpos;
- currpos = lseek(fd, 0, SEEK_CUR);
off_t currpos;
currpos = lseek(fd, 0, SEEK_CUR);
- #include <fcntl.h>
- #include <stdio.h>
- int main(void){
- int fd,byteNum,result;
- char wbuf[10] = "123456789";
- char rbuf[10];
- if((fd = open("./a.txt", O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IXUSR))<0){
- perror("open");
- return -1;
- }
- if((byteNum = write(fd, wbuf, 10))<0){
- perror("write");
- return -1;
- }
- if((result = lseek(fd, 4, SEEK_SET))<0){
- perror("lseek");
- return -1;
- }
- if((byteNum = read(fd, rbuf, 10)) < 0){
- perror("read");
- return -1;
- }
- printf("read content:%s\n",rbuf);
- close(fd);
- return 0;
- }
#include <fcntl.h>
#include <stdio.h>
int main(void){
int fd,byteNum,result;
char wbuf[10] = "123456789";
char rbuf[10];
if((fd = open("./a.txt", O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IXUSR))<0){
perror("open");
return -1;
}
if((byteNum = write(fd, wbuf, 10))<0){
perror("write");
return -1;
}
if((result = lseek(fd, 4, SEEK_SET))<0){
perror("lseek");
return -1;
}
if((byteNum = read(fd, rbuf, 10)) < 0){
perror("read");
return -1;
}
printf("read content:%s\n",rbuf);
close(fd);
return 0;
}
运行结果: - #include <fcntl.h>
- #include <stdio.h>
- int main(void){
- int fd,byteNum,result;
- char wbuf[10] = "123456789";
- char rbuf[10];
- if((fd = open("./a.txt", O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IXUSR))<0){
- perror("open");
- return -1;
- }
- if((byteNum = write(fd, wbuf, 10))<0){
- perror("write");
- return -1;
- }
- if((result = lseek(fd, -1, SEEK_SET))<0){
- perror("lseek");
- return -1;
- }
- if((byteNum = read(fd, rbuf, 10)) < 0){
- perror("read");
- return -1;
- }
- printf("read content:%s\n",rbuf);
- close(fd);
- return 0;
- }
#include <fcntl.h>
#include <stdio.h>
int main(void){
int fd,byteNum,result;
char wbuf[10] = "123456789";
char rbuf[10];
if((fd = open("./a.txt", O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IXUSR))<0){
perror("open");
return -1;
}
if((byteNum = write(fd, wbuf, 10))<0){
perror("write");
return -1;
}
if((result = lseek(fd, -1, SEEK_SET))<0){
perror("lseek");
return -1;
}
if((byteNum = read(fd, rbuf, 10)) < 0){
perror("read");
return -1;
}
printf("read content:%s\n",rbuf);
close(fd);
return 0;
}
运行结果: - #include <fcntl.h>
- #include <stdio.h>
- int main(void){
- int fd,byteNum,result;
- char wbuf[10] = "123456789";
- char rbuf[10];
- if((fd = open("./a.txt", O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IXUSR))<0){
- perror("open");
- return -1;
- }
- if((byteNum = write(fd, wbuf, 10))<0){
- perror("write");
- return -1;
- }
- if((result = lseek(fd, -4, SEEK_CUR))<0){
- perror("lseek");
- return -1;
- }
- if((byteNum = read(fd, rbuf, 10)) < 0){
- perror("read");
- return -1;
- }
- printf("read content:%s\n",rbuf);
- close(fd);
- return 0;
- }
#include <fcntl.h>
#include <stdio.h>
int main(void){
int fd,byteNum,result;
char wbuf[10] = "123456789";
char rbuf[10];
if((fd = open("./a.txt", O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IXUSR))<0){
perror("open");
return -1;
}
if((byteNum = write(fd, wbuf, 10))<0){
perror("write");
return -1;
}
if((result = lseek(fd, -4, SEEK_CUR))<0){
perror("lseek");
return -1;
}
if((byteNum = read(fd, rbuf, 10)) < 0){
perror("read");
return -1;
}
printf("read content:%s\n",rbuf);
close(fd);
return 0;
}
运行结果: - #include <fcntl.h>
- #include <stdio.h>
- int main(void){
- int fd,byteNum,result;
- char wbuf[10] = "123456789";
- char rbuf[10];
- if((fd = open("./a.txt", O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR))<0){
- perror("open");
- return -1;
- }
- if((byteNum = write(fd, wbuf, 10))<0){
- perror("write");
- return -1;
- }
- if((result = lseek(fd, 40960, SEEK_END))<0){
- perror("lseek");
- return -1;
- }
- if((byteNum = write(fd, wbuf, 10)) < 0){
- perror("write");
- return -1;
- }
- close(fd);
- return 0;
- }
#include <fcntl.h>
#include <stdio.h>
int main(void){
int fd,byteNum,result;
char wbuf[10] = "123456789";
char rbuf[10];
if((fd = open("./a.txt", O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR))<0){
perror("open");
return -1;
}
if((byteNum = write(fd, wbuf, 10))<0){
perror("write");
return -1;
}
if((result = lseek(fd, 40960, SEEK_END))<0){
perror("lseek");
return -1;
}
if((byteNum = write(fd, wbuf, 10)) < 0){
perror("write");
return -1;
}
close(fd);
return 0;
}
运行结果: 0000020 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
*
0120000 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 1 2 3 4 5 6
0120020 7 8 9 \0
0120024
- #include <stdio.h>
- #include <fcntl.h>
- int main(void){
- int fd,result;
- char wbuf[5] = "1234";
- if((fd=open("a.txt",O_WRONLY|O_APPEND))<0){
- perror("open");
- return -1;
- }
- if((result = lseek(fd, 2, SEEK_SET)) < 0){
- perror("lseek");
- return -1;
- }
- if((result = write(fd, wbuf, 4))<0){
- perror("write");
- return -1;
- }
- close(fd);
- return 0;
- }
#include <stdio.h>
#include <fcntl.h>
int main(void){
int fd,result;
char wbuf[5] = "1234";
if((fd=open("a.txt",O_WRONLY|O_APPEND))<0){
perror("open");
return -1;
}
if((result = lseek(fd, 2, SEEK_SET)) < 0){
perror("lseek");
return -1;
}
if((result = write(fd, wbuf, 4))<0){
perror("write");
return -1;
}
close(fd);
return 0;
}
程序执行前a.txt为:123456789 - #include <stdio.h>
- #include <fcntl.h>
- int main(void){
- int fd,result;
- char wbuf[5] = "1234";
- if((fd=open("a.txt",O_WRONLY|O_APPEND))<0){
- perror("open");
- return -1;
- }
- if((result = lseek(fd, -2, SEEK_END)) < 0){
- perror("lseek");
- return -1;
- }
- if((result = write(fd, wbuf, 4))<0){
- perror("write");
- return -1;
- }
- close(fd);
- return 0;
- }
#include <stdio.h>
#include <fcntl.h>
int main(void){
int fd,result;
char wbuf[5] = "1234";
if((fd=open("a.txt",O_WRONLY|O_APPEND))<0){
perror("open");
return -1;
}
if((result = lseek(fd, -2, SEEK_END)) < 0){
perror("lseek");
return -1;
}
if((result = write(fd, wbuf, 4))<0){
perror("write");
return -1;
}
close(fd);
return 0;
}
运行结果: 0000000 1 2 3 \n
0000004
yan@yan-vm:~/ctest$ ./a.out
yan@yan-vm:~/ctest$ od -c a.txt
0000000 1 2 3 \n 1 2 3 4
0000010
这篇关于《UNIX环境高级编程》笔记--read函数,write函数,lseek函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!