Chinaunix

标题: lseek返回值问题 [打印本页]

作者: FinalBSD    时间: 2008-01-22 14:31
标题: lseek返回值问题
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main(void)
{
        int fd;
        char buf[20];
        int size;
        off_t location;
        char * filename = "tt.dat";

        if( (fd = open(filename,O_RDWR | O_CREAT | O_TRUNC)) == -1){
                perror("open");
                return 1;
        }

        size = write(fd,"Hello C Programming!\n",20);
        if(size <0){
                perror("write");
                return 1;
        }


        printf("%d bytes write to %s successfully!\n",size,filename);
        printf("the offset now is: %d\n",lseek(fd,0,SEEK_CUR));
        if( (location = lseek(fd,5,SEEK_SET) > -1)){
                printf("after lseeked %d .the offset now is: %d\n",location,lseek(fd,0,SEEK_CUR));
        }

        size = read(fd,buf,5);
        if(size <0){
                perror("read");
                return 1;
        }
        printf("%d bytes(\"%s\") read from  %s successfully!\n",size,buf,filename);
        printf("the offset now is: %d\n",lseek(fd,0,SEEK_CUR));
        return 0;
}

返回:
20 bytes write to tt.dat successfully!
the offset now is: 20
after lseeked 1 .the offset now is: 0
5 bytes(" C Pr") read from  tt.dat successfully!
the offset now is: 10

问:after lseeked 1 .the offset now is: 0为什么不是预期要得到的:after lseeked 5 .the offset now is: 5

[ 本帖最后由 FinalBSD 于 2008-1-22 14:52 编辑 ]
作者: cugb_cat    时间: 2008-01-22 14:37
标题: 回复 #1 FinalBSD 的帖子
因为你这句的括号括错地方了

  1. if( (location = lseek(fd,5,SEEK_SET) > -1)){
  2.                 printf("after lseeked %d .the offset now is: %d\n",location,lseek(fd,0,SEEK_CUR));
  3.         }
复制代码

作者: FinalBSD    时间: 2008-01-22 14:39
原帖由 cugb_cat 于 2008-1-22 14:37 发表
因为你这句的括号括错地方了

if( (location = lseek(fd,5,SEEK_SET) > -1)){
                printf("after lseeked %d .the offset now is: %d\n",location,lseek(fd,0,SEEK_CUR));
        }

果然是,编译居然没错误。。谢谢啦!

[ 本帖最后由 FinalBSD 于 2008-1-22 14:40 编辑 ]
作者: cugb_cat    时间: 2008-01-22 14:42
标题: 回复 #3 FinalBSD 的帖子
这样写是合法的,为什么会有错误啊?
作者: FinalBSD    时间: 2008-01-22 14:46
有新问题啊:
GNU/Linux下得到预期的输出:
after lseeked 5 .the offset now is: 5
FreeBSD下得到
after lseeked 5 .the offset now is: 0

man 里面都说:Upon successful completion, lseek() returns the resulting offset location
     as measured in bytes from the beginning of the file.

纳闷呢
作者: cugb_cat    时间: 2008-01-22 14:54
标题: 回复 #5 FinalBSD 的帖子
这可能是不同编译器求值顺序不同的缘故?
你把lseek(SEEK_CUR)放到一个单独的语句中试试?

[ 本帖最后由 cugb_cat 于 2008-1-22 14:56 编辑 ]
作者: FinalBSD    时间: 2008-01-22 14:56
原帖由 cugb_cat 于 2008-1-22 14:54 发表
这可能是不同编译器求值顺序不同的缘故?
你把lseek(SEEK_CUR)放到一个单独的语句中试试?

3和5行输出又是对的,诡异吧,不管它了,貌似这个问题很无聊,谢谢啦
作者: FinalBSD    时间: 2008-01-22 14:59
location2 = lseek(fd,0,SEEK_CUR);这样单独写之后还是不对
作者: 熏红猴    时间: 2008-01-22 15:01
可能实现不一样吧,这个是系统调用,应该跟内核有关。
作者: tyc611    时间: 2008-01-22 18:37
LZ没看懂cugb_cat给的提示,你的程序错误在于这句
if( (location = lseek(fd,5,SEEK_SET) > -1)){

应该改为
if( (location = lseek(fd,5,SEEK_SET)) > -1){

因为关系运算符<比赋值运算符=优先级高,你的语句相当于把lseek函数返回值与-1作比较,再将比较结果(布尔值)赋值给location变量,造成了你的“错误”

[ 本帖最后由 tyc611 于 2008-1-22 18:38 编辑 ]
作者: eplinux    时间: 2008-07-26 20:41
标题: 楼上正解
楼上正解 这种错误不仔细了还真的好难找
作者: baicj    时间: 2008-07-27 00:30
lseek不是这样用的。比如在我macosx上,lseek(fd,0,SEEK_CUR)只会返回0。
作者: baicj    时间: 2008-07-27 00:32
没注意,原来是有人挖坟




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2