read函数的功能是向以打开的文件读取数据。
read函数需要包含头文件 :#include <unistd.h>。
read函数的原型为:
ssize_t read(int fd, void *buf, size_t count);
其中,fd为文件描述符;buf表示读出数据缓冲区地址;count表示读出的字节数。
返回值:若读取成功,则返回读到的字节数;若失败,返回-1;若已达到文件尾,则返回0。因此读到的字节数可能小于count的值。
#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 = "luyi is handsome!!";
fd = open("./file1",O_RDWR);
printf("fd = %d\n",fd);
if(fd == -1){
printf("open file1 fail!!\n");
fd = open("./file1",O_RDWR|O_CREAT,0600);
if(fd > 0){
printf("create file1 success!!!\n");
}else if(fd == -1){
printf("creat file1 success!!\n");
}
}else{
printf("open file1 success!!!\n");
}
int n_write = write(fd,buf,strlen(buf));
if(n_write != -1){
printf("write %d byte to file1\n",n_write);
}else{
printf("write fail!!!\n");
}
char *readBuf;
readBuf = (char*)malloc(sizeof(char)*n_write);
close(fd);
open("./file1",O_RDWR);
//ssize_t read(int fd, void *buf, size_t count);
int n_read = read(fd,readBuf,n_write);
printf("n_read = %d,context = %s\n",n_read,readBuf);
close(fd);
return 0;
}
在这个程序中,在调用read函数之前,先调用了close函数和open函数,这是为了让光标移到文件的头,否则将读取失败。因此,就还需要用到lseek函数来移动文件中光标的位置。
通过调用lseek函数可以改变光标的位置,其函数原型为
off_t lseek(int fd, off_t offset, int whence);
其中,fd为文件描述符;offset指的是每一次读写操作所需移动距离,以字节为单位 ,可正可负,正值表示想文件尾部移动,负值表示向文件头部移动。whence表示当前位置的基点,主要有以下三个基点符号常量。
SEEK_SEK:将光标移到距离文件头出offset个字节;
SEEK_CUR:将光标移到当前位置前后offset个字节;
SEEK_END:将光标移到文件末尾前后offset个字节。
例如:
#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 = "luyi is handsome!!";
fd = open("./file1",O_RDWR);
printf("fd = %d\n",fd);
if(fd == -1){
printf("open file1 fail!!\n");
fd = open("./file1",O_RDWR|O_CREAT,0600);
if(fd > 0){
printf("create file1 success!!!\n");
}else if(fd == -1){
printf("creat file1 success!!\n");
}
}else{
printf("open file1 success!!!\n");
}
int n_write = write(fd,buf,strlen(buf));
if(n_write != -1){
printf("write %d byte to file1\n",n_write);
}else{
printf("write fail!!!\n");
}
char *readBuf;
readBuf = (char*)malloc(sizeof(char)*n_write);
// close(fd);
// open("./file1",O_RDWR);
// off_t lseek(int fd, off_t offset, int whence);
// lseek(fd,0,SEEK_SET);
// lseek(fd,-1*n_write,SEEK_CUR);
lseek(fd,-1*n_write,SEEK_END);
// ssize_t read(int fd, void *buf, size_t count);
int n_read = read(fd,readBuf,n_write);
printf("n_read = %d,context = %s\n",n_read,readBuf);
close(fd);
return 0;
}
除此之外,lseek函数还可以用来计算文件大小,因为他的返回值是以字节为单位,从文件的起始点开始计算到当前位置的字节数。
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int fd;
char *file = "lubenwei is very niubi!!!";
fd = open("./file",O_RDWR);
printf("fd1 = %d\n",fd);
write(fd,file,strlen(file));
fd = open("./file",O_RDWR);
int size_of_file = lseek(fd,0,SEEK_END);
printf("size_of_file = %d\n",size_of_file);
close(fd);
putchar('\n');
return 0;
}