Linux⽂件编程read函数
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);
write的返回值其中,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;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。