LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1135|回复: 4

read函数返回的问题

[复制链接]
发表于 2007-7-31 22:52:56 | 显示全部楼层 |阅读模式
代码中read函数的返回值总是不正确,下面是代码
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "skyeye_nandflash.h"
#include "nandflash_smallblock.h"
.......
void  nandflash_sb_setup(struct nandflash_device* dev)
{
        u8 flag=0xFF;
        int len,start,needinit=0;
        struct stat statbuf;
        struct nandflash_sb_status *nf;
        int i;
        ....
        .....
       if ((nf->fdump= open(dev->dump, O_RDWR | O_CREAT)) < 0)  //打开文件dev->dump
       {
               free(nf);
               printf("error open nandflash dump!\n");
               skyeye_exit(-1);
       }
        .....
}


void nandflash_sb_doaddr(struct nandflash_sb_status *nf)
{
.................
#if (defined(__MINGW32__))
        int block=nf->address/528;
        int tmp;
        if(block!=nf->curblock){
        if (nf->address<nf->memsize)
        {
          memset(nf->readbuffer,0xff,528);                                               
          if(lseek(nf->fdump,block*528,SEEK_SET)==-1)
          printf("lseek error\n");
          printf("offset:%d",block);
          tmp=read(nf->fdump,nf->readbuffer,528);        //这里的返回值总不正确,但好像内容是正确的读出来了
          if  (tmp!=528)
          printf("read error address:%08x,readsize:%d,block:%d\n",nf->address,tmp,block);
         }
        else
         {
           printf("read outof bound\n");
        }
                                //}
#endif
..................
}

下面是打印输出
big_endian is false.
uart_mod:0, desc_in:, desc_out:, converter:
Loaded RAM   ./u-boot.bin
Loaded RAM   ./uImage
offset:0
read error address:00000000,readsize:359,block:0
offset:1
offset:2
read error address:00000420,readsize:211,block:2
offset:3
offset:4
offset:5
read error address:00000a50,readsize:55,block:5
offset:6
read error address:00000c60,readsize:415,block:6
发表于 2007-8-8 10:01:09 | 显示全部楼层
我也要好好学习
回复 支持 反对

使用道具 举报

发表于 2007-8-8 11:46:58 | 显示全部楼层
read成功返回值是0。如果大于0,则需要判断是碰到 EOF 还是出错了。
  1. $ man read
  2. ...
  3. EXIT STATUS
  4.        The following exit values shall be returned:
  5.         0     Successful completion.
  6.        >0     End-of-file was detected or an error occurred.
复制代码
回复 支持 反对

使用道具 举报

发表于 2007-8-9 23:14:28 | 显示全部楼层
>0     End-of-file was detected or an error occurred.
应该是<0吧,
如果返回的是大于0的话,那是接受的字符数。
回复 支持 反对

使用道具 举报

发表于 2007-10-12 17:24:35 | 显示全部楼层
这种东西man一下不就有了。man 2 read
<code>
       On  success, the number of bytes read is returned (zero indicates end of file), and the
       file position is advanced by this number.  It is not an error if this number is smaller
       than the number of bytes requested; this may happen for example because fewer bytes are
       actually available right now (maybe because we were close to end-of-file, or because we
       are  reading  from  a pipe, or from a terminal), or because read() was interrupted by a
       signal.  On error, -1 is returned, and errno is set appropriately. In this case  it  is
       left unspecified whether the file position (if any) changes.

</code>
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表