非法寻求 [英] illegal seek

查看:63
本文介绍了非法寻求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码给出了非法搜索错误。关闭()致电

这是什么错误,什么时候到来?


main()

{

int fd,num;

char buf [150];

fd = open(" 123.c",O_RDWR);

if(fd!= - 1)

{


printf(" fd =%d的文件打开了\ n ,fd);

num = read(fd,buf,150);

printf(" num =%d \ nREAD:%s",num,buf );

perror(READ);

close(fd);

perror(" CLOSE");

}

}

this code gives an "illegal seek error" on close() call
what is this error and when does it come?

main()
{
int fd,num;
char buf[150];
fd = open ("123.c",O_RDWR);
if(fd!=-1)
{

printf("a file with fd=%d is opened\n",fd);
num=read(fd,buf,150);
printf("num=%d\nREAD:%s",num,buf);
perror("READ");
close(fd);
perror("CLOSE");
}
}

推荐答案

文章< 05 ********* ******** @ p25g2000pri。 googlegroups.com>,

smarty< cs ******* @ gmail.comwrote:
In article <05**********************************@p25g2000pri. googlegroups.com>,
smarty <cs*******@gmail.comwrote:

>此代码给出非法寻找错误关闭()调用
这是什么错误,什么时候出现?
>this code gives an "illegal seek error" on close() call
what is this error and when does it come?



你的代码包含几个错误,特别是从buf打印一个潜在的

未终止的字符串。


但是你要问的特殊问题似乎是,b $ b Linux的perror()经常改变错误。我不认为这是合法的,

但是我手头没有标准。


- Richard

-

:wq

Your code contains several errors, notably printing a potentially
unterminated string from buf.

But the particular problem you are asking about seems to be that
Linux''s perror() often changes errno. I don''t think this is legal,
but I don''t have the standard to hand.

-- Richard
--
:wq


>
>

但你问的特殊问题似乎是

Linux的perror()经常改变错误。我不认为这是合法的,

但是我手头没有标准。
But the particular problem you are asking about seems to be that
Linux''s perror() often changes errno. I don''t think this is legal,
but I don''t have the standard to hand.



好​​吧,标准库函数可以将errno设置为非零

函数,除非

标准中记录了errno的使用(即使没有

错误)。

perror()中errno的使用在标准中记录为使用



值。


顺便说一句,读/打开/关闭功能不是标准库

函数。

所以对它们一无所知。我不太记得posix

规格,

但是我不确定errno是否可以在成功时给出正值。

这些函数的返回值可能更好地表明错误

条件。

标准的fopen / fread / fclose函数同样可以做到

任务,我想。然后你也有了恐惧。

在标准库之前放置errno = 0,有文件使用



errno和阅读errno在我看来也是合法的。


Szabolcs

Well, a standard library function may set errno to a non-zero
function, unless
the use of errno is documented in the standard (even if there was no
error).
The use of errno in perror() is documented in the standard as using
its
value.

By the way, the read/open/close functions are not standard library
functions.
So nothing is known about them. I don''t quite remember the posix
specifications,
but I am not sure if errno can put to a positive value on success.
The return value of these functions might better indicate the error
condition.
The standard fopen/fread/fclose functions might equally well do the
task, I''d think. Then you have ferror, too.
Putting errno=0 before a standard library, that has a documented use
of
errno, and reading errno then seems to me legal, too.

Szabolcs


在文章< 79 ******** ******** @ a70g2000hsh。 googlegroups.com>,

Szabolcs Borsanyi< bo ****** @thphys.uni-heidelberg.dewrote:
In article <79**********************************@a70g2000hsh. googlegroups.com>,
Szabolcs Borsanyi <bo******@thphys.uni-heidelberg.dewrote:

> ;顺便说一下,读/打开/关闭功能不是标准的库函数。
所以对它们一无所知。我不太记得posix
规格,但我不确定errno是否可以在成功时发挥积极作用。
>By the way, the read/open/close functions are not standard library
functions.
So nothing is known about them. I don''t quite remember the posix
specifications,
but I am not sure if errno can put to a positive value on success.



使用这些函数与perror()问题无关。

以下程序在Linux上表现出相同的行为:


#include< stdio.h>


int main(无效)

{

perror(one);

perror(two);

返回0;

}


当我在这里运行它打印


一:成功

二:非法寻求


- Richard

-

:wq

The use of those functions is irrelevant to the perror() problem.
The following program exhibits the same behavious on Linux:

#include <stdio.h>

int main(void)
{
perror("one");
perror("two");
return 0;
}

When I run it here it prints

one: Success
two: Illegal seek

-- Richard
--
:wq


这篇关于非法寻求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆