免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3423 | 回复: 5
打印 上一主题 下一主题

[C] 为什么lseek的返回值不能转换成int呢? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-12-20 09:49 |只看该作者 |倒序浏览
编译的提示如下:
大概意思是说无效的左值..难道off_t 类型不能转换成int?


#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<errno.h>

void my_err(const char *err_string,int line)
{
&nbsp;&nbsp;fprintf(stderr,"line:%d ",line);
&nbsp;&nbsp;perror(err_string);
&nbsp;&nbsp;exit(1);
}

int my_read(int fd)
{
&nbsp;&nbsp;int ret,i,len;
&nbsp;&nbsp;char read_buf[64];

&nbsp;&nbsp;if(lseek(fd,0,SEEK_END)==-1)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my_err("lseek",__LINE__);
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;if((len=lseek(fd,0,SEEK_CUR))=-1)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my_err("lseek",__LINE__);
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;if((lseek(fd,0,SEEK_SET))==-1)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my_err("lseek",__LINE__);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;printf("len :%d\n",len);

&nbsp;&nbsp;if((ret=read(fd,read_buf,len))<0)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my_err("read",__LINE__);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;for(i=0;i<len;i++)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%c",read_buf[i]);
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;printf("\n");

&nbsp;&nbsp;return ret;
}

int main(void)
{
&nbsp;&nbsp;int fd;
&nbsp;&nbsp;char write_buf[32]="Hello World!";

&nbsp;&nbsp;if((fd=open("test01.c",O_RDWR|O_CREAT|O_TRUNC,S_IRWXU))==-1)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my_err("open",__LINE__);
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;else
&nbsp;&nbsp;&nbsp;&nbsp;{printf("create file success\n");
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;if(write(fd,write_buf,strlen(write_buf))!=strlen(write_buf))
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my_err("write",__LINE__);
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;my_read(fd);

&nbsp;&nbsp;printf("/*----------------*/\n");
&nbsp;&nbsp;if(lseek(fd,10,SEEK_END)==-1)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my_err("lseek",__LINE__);
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;if(write(fd,write_buf,strlen(write_buf))!=strlen(write_buf))
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my_err("write",__LINE__);
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;my_read(fd);

&nbsp;&nbsp;close(fd);
&nbsp;&nbsp;return 0;
}

论坛徽章:
0
2 [报告]
发表于 2008-12-20 09:51 |只看该作者
len=lseek(fd,0,SEEK_CUR))=-1

len=lseek(fd,0,SEEK_CUR))==-1

论坛徽章:
0
3 [报告]
发表于 2008-12-20 10:08 |只看该作者
原帖由 雨过白鹭洲 于 2008-12-20 09:51 发表
len=lseek(fd,0,SEEK_CUR))=-1

len=lseek(fd,0,SEEK_CUR))==-1

什么意思?

论坛徽章:
0
4 [报告]
发表于 2008-12-20 10:10 |只看该作者
这还没看懂?

你两个赋值放一起了,-1怎么能赋给lseek返回的off_t值呢?

论坛徽章:
0
5 [报告]
发表于 2008-12-20 10:18 |只看该作者
哦,我明白了....郁闷,又犯了这样的错误....

论坛徽章:
1
天蝎座
日期:2013-08-25 10:27:22
6 [报告]
发表于 2008-12-20 15:45 |只看该作者
好好练练眼力。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP