read的返回值

linux 下read函數返回值分析 (2011-11-29 11:17:23)
標籤: linux read 返回值 雜談 分類: ARM-編程筆記
原文出處:http://blog.chinaunix.net/space.php?uid=20558494&do=blog&id=2803003
read函數是Linux下不帶緩存的文件I/O操作函數之一,所謂的不帶緩存是指一個函數只調用系統中的一個函數。另外還有open、write、lseek、close,它們雖然不是ANSI C的組成部分,但是POSIX的組成部分。

在對read的使用過程中,發現對其返回值的處理比較重要,這裏做一下總結。

read函數原型:
ssize_t read(int fd,void *buf,size_t count)

函數返回值分爲下面幾種情況:
1、如果讀取成功,則返回實際讀到的字節數。這裏又有兩種情況:一是如果在讀完count要求字節之前已經到達文件的末尾,那麼實際返回的字節數將 小於count值,但是仍然大於0;二是在讀完count要求字節之前,仍然沒有到達文件的末尾,這是實際返回的字節數等於要求的count值。
2、如果讀取時已經到達文件的末尾,則返回0。
3、如果出錯,則返回-1。
這樣也就是說分爲>0 <0 =0三種情況進行討論。在有的時候,<0 =0可以合爲一種情況進行處理。這要根據程序的需要進行處理。
實例分析:
-------------------------------

include

include

include

include

include

include

include

define MAXSIZE 35

int main(void)
{
int i,j,fd,size,len;
char *buf=”Hello!Im writing to this file!";
char buf_r[MAXSIZE];
len=strlen(buf);
//open
if((fd=open("/tmp/hello.c",O_CREAT | O_TRUNC | O_RDWR,0666))<0) {
perror("open:");
exit(1);
}
else
printf("Open file:hello.c %d\n",fd);
//write
if((size=write(fd,buf,len))<0){
perror("write:");
exit(1);
}
else
printf("Write:%s\n\n\n",buf);
//test-read
printf("Now test starts...\n\n");
for(i=0;i<20;i++){
lseek(fd,0,SEEK_SET);
for(j=0;j<MAXSIZE;j++)
buf_r[j]=0;
if((size=read(fd,buf_r,MAXSIZE-i))<0){
perror("read:");
exit(1);
}
else {
buf_r[MAXSIZE-i]='\0';
printf("string-len=%d,count=%d,size=%d\n",len,MAXSIZE-i,size);
printf("read from file:%s \n",buf_r);
}
}
printf("\n\nNow test stops...\n");
//close
if(close(fd)<0){
perror("close:");
exit(1);
}
else
printf("close hello.c\n");
exit(0);
}
-------------------------------
結果如下:
-------------------------------
[armlinux@lqm test-read]$ ./write
Open file:hello.c 3
Write:Hello!I
m writing to this file!

Now test starts…
string-len=31,count=35,size=31
read from file:Hello!Im writing to this file!
string-len=31,count=34,size=31
read from file:Hello!I
m writing to this file!
string-len=31,count=33,size=31
read from file:Hello!Im writing to this file!
string-len=31,count=32,size=31
read from file:Hello!I
m writing to this file!
string-len=31,count=31,size=31
read from file:Hello!Im writing to this file!
string-len=31,count=30,size=30
read from file:Hello!I
m writing to this file
string-len=31,count=29,size=29
read from file:Hello!Im writing to this fil
string-len=31,count=28,size=28
read from file:Hello!I
m writing to this fi
string-len=31,count=27,size=27
read from file:Hello!Im writing to this f
string-len=31,count=26,size=26
read from file:Hello!I
m writing to this
string-len=31,count=25,size=25
read from file:Hello!Im writing to this
string-len=31,count=24,size=24
read from file:Hello!I
m writing to thi
string-len=31,count=23,size=23
read from file:Hello!Im writing to th
string-len=31,count=22,size=22
read from file:Hello!I
m writing to t
string-len=31,count=21,size=21
read from file:Hello!Im writing to
string-len=31,count=20,size=20
read from file:Hello!I
m writing to
string-len=31,count=19,size=19
read from file:Hello!Im writing t
string-len=31,count=18,size=18
read from file:Hello!I
m writing
string-len=31,count=17,size=17
read from file:Hello!Im writing
string-len=31,count=16,size=16
read from file:Hello!I
m writin

Now test stops…
close hello.c
-------------------------------
現象:
測試部分中,string-len是測試文件內容的長度,count是要求讀取的字節數,size是實際讀取的字節數。可以觀察出,開始 count>string-len,所以雖然讀取成功,但是返回的實際字節數要小於要求的字節數。從count=string-len之後,實際返 回的字節數等於要求的字節數。
問題分析:
1、每次執行read函數之前,保證指定好起始位置,並且對buf初始化。
如果將
lseek(fd,0,SEEK_SET);
for(j=0;j

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章