473,396 Members | 2,098 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

illegal seek

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");
}
}
Jun 27 '08 #1
22 7203
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?
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
Jun 27 '08 #2
>
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.
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
Jun 27 '08 #3
In article <79**********************************@a70g2000hsh. googlegroups.com>,
Szabolcs Borsanyi <bo******@thphys.uni-heidelberg.dewrote:
>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 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
Jun 27 '08 #4
ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
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?

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.
Hmmm... The standard says:

"The value of errno may be set to nonzero by a library function call
whether or not there is an error, provided the use of errno is not
documented in the description of the function in this International
Standard."

perror "documents the use of errno" so it seems it is not permitted to
change it. Of course, if the intent of that phrase it to permit
functions to change errno provided they are not documented as
*setting* it in some specific way, then perror *is* allowed to set it
nonzero. But then, why use the obviously broad phrase "use of"?

In practise, it hardly matters. One should only rely on the value
immediately following a function call that has failed in a way that
documents the setting of errno and this is the problem the OP is
having. Even if perror stuck to the letter of the standard, both the
printf and the close call can make it nonzero (even if there is no
error).

--
Ben.
Jun 27 '08 #5
smarty wrote:
>
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");
}
}
Who knows. There are no such functions as 'open', 'read', 'close'
in standard C. Look up, and use, fopen and fclose, and check the
various standard library calls for means of reading numbers.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

** Posted from http://www.teranews.com **
Jun 27 '08 #6
In article <48***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
>Who knows. There are no such functions as 'open', 'read', 'close'
in standard C.
As it turns out, it's nothing to do with those functions.

-- Richard
--
:wq
Jun 27 '08 #7

"smarty" <cs*******@gmail.comschrieb im Newsbeitrag
news:05**********************************@p25g2000 pri.googlegroups.com...
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");
}
}
close() sets errno if (and only if) it fails, which it indicates by
returning -1.
printf() returns a negative value on failure.
Only use perror() directly after a failed function call that is documented
to set errno on failure or set errno to 0 prior to calling that function.

Bye, Jojo
Jun 27 '08 #8
CBFalconer <cb********@yahoo.comwrites:
smarty wrote:
>>
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");
}
}

Who knows. There are no such functions as 'open', 'read', 'close'
in standard C. Look up, and use, fopen and fclose, and check the
various standard library calls for means of reading numbers.
Those are POSIX functions. The OP might well have a good reason to
use them instead of the C standard functions fopen, fread, and fclose
(the POSIX functions provide some features that the standard C
functions don't). The quoted program doesn't demonstrate any such
reason, but it's obviously just a sample.

But yes, it's generally better to use the standard C I/O functions
*unless* you have a specific reason to use the POSIX functions and pay
the price of losing some portability.

(Incidentally, the program has nothing to do with reading numbers; num
is the number of bytes read.)

Some incidental advice for the original poster:

Write "int main(void)" rather than "main()".

Adding a blank after each comma, after the "if" keyword, and before
and after each binary operator, aids readability.

Since main returns an int, it should do so: add "return 0;" before the
closing brace. There are circumstances in which this is not necessary
(if you're using a C99 implementation, or if you don't care about the
status returned to the host environment), but it never hurts, and it's
a good habit.

And, of course, you should examine the value of errno only after you
know a function has failed (but check the function's documentation to
find out whether it even sets errno).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #9
Richard Tobin wrote:
CBFalconer <cb********@maineline.netwrote:
>Who knows. There are no such functions as 'open', 'read', 'close'
in standard C.

As it turns out, it's nothing to do with those functions.
Since it is not written in C, who knows what is wrong with it. The
point is that it contains calls to unknown functions. That makes
it off-topic in c.l.c.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #10
Joachim Schmitz wrote:
>
.... snip ...
>
close() sets errno if (and only if) it fails, which it indicates
by returning -1.
The following is a legal definition of close:

int close(int parm) {
if (-1 == parm) parm++;
return parm;
}

which doesn't meet your description. The point is that close is
undefined in standard C. In this newsgroup the discussion stops
there.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

** Posted from http://www.teranews.com **
Jun 27 '08 #11
CBFalconer said:
Richard Tobin wrote:
>CBFalconer <cb********@maineline.netwrote:
>>Who knows. There are no such functions as 'open', 'read', 'close'
in standard C.

As it turns out, it's nothing to do with those functions.

Since it is not written in C,
It sure looks like C to me.
who knows what is wrong with it.
comp.unix.programmer knows what's wrong with it.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #12
On 14 May 2008 12:10:58 GMT, ri*****@cogsci.ed.ac.uk (Richard Tobin)
wrote in comp.lang.c:
In article <79**********************************@a70g2000hsh. googlegroups.com>,
Szabolcs Borsanyi <bo******@thphys.uni-heidelberg.dewrote:
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 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
In that case, the problem is system-specific, and he should take it up
with the Linux developers. It is a QOI issue, at best, since the
standard allows perror() to modify errno so long as it uses the
original value to produce output before modifying it.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jun 27 '08 #13
On 14 May 2008 at 16:39, CBFalconer wrote:
The following is a legal definition of close:

int close(int parm) {
if (-1 == parm) parm++;
return parm;
}
Wrong. It fails to meet the specification for close():

"The close() function shall deallocate the file descriptor indicated
by fildes. To deallocate means to make the file descriptor available for
return by subsequent calls to open() or other functions that allocate
file descriptors. All outstanding record locks owned by the process on
the file associated with the file descriptor shall be removed (that is,
unlocked)."

Jun 27 '08 #14
Antoninus Twink wrote:
On 14 May 2008 at 16:39, CBFalconer wrote:
>The following is a legal definition of close:

int close(int parm) {
if (-1 == parm) parm++;
return parm;
}

Wrong. It fails to meet the specification for close():

"The close() function shall deallocate the file descriptor indicated
by fildes. To deallocate means to make the file descriptor available
for return by subsequent calls to open() or other functions that
allocate file descriptors. All outstanding record locks owned by the
process on the file associated with the file descriptor shall be
removed (that is, unlocked)."
Guess you missed his point: close() is not in the C-Standard, hence there is
no specification.
Chuck simply ignores that close() exists in POSIX and is available to the
vast majority of implementations. He also ignored the fact that the OP did
use close() and that the most likely reason is that this is because it is
provided by his implementation.

Bye, Jojo
Jun 27 '08 #15
Joachim Schmitz wrote:
Antoninus Twink wrote:
>CBFalconer wrote:
>>The following is a legal definition of close:

int close(int parm) {
if (-1 == parm) parm++;
return parm;
}

Wrong. It fails to meet the specification for close():
.... snip posix? definition ...
>
Guess you missed his point: close() is not in the C-Standard,
hence there is no specification. Chuck simply ignores that
close() exists in POSIX and is available to the vast majority
of implementations. He also ignored the fact that the OP did
use close() and that the most likely reason is that this is
because it is provided by his implementation.
Of course the Twink-troll also snipped my explanation, which
follows below:
>>which doesn't meet your description. The point is that
close is undefined in standard C. In this newsgroup the
discussion stops there.
which is part of the trolls standard mechanism, aiming to disturb
the normal operation of the newsgroup. I see no reason to ever use
open, close and read, when fopen, fclose and fread are available
everywhere, and portable.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #16
On May 16, 7:39*am, "Joachim Schmitz" <nospam.j...@schmitz-digital.de>
wrote:
Antoninus Twink wrote:
On 14 May 2008 at 16:39, CBFalconer wrote:
The following is a legal definition of close:
* *int close(int parm) {
* * * if (-1 == parm) parm++;
* * * return parm;
* *}
Wrong. It fails to meet the specification for close():
"The close() function shall deallocate the file descriptor indicated
by fildes. To deallocate means to make the file descriptor available
for return by subsequent calls to open() or other functions that
allocate file descriptors. All outstanding record locks owned by the
process on the file associated with the file descriptor shall be
removed (that is, unlocked)."

Guess you missed his point: close() is not in the C-Standard, hence there is
no specification.
Chuck simply ignores that close() exists in POSIX and is available to the
vast majority of implementations.
but not all. Since when was Posix on-topic to comp.lang.c
He also ignored the fact that the OP did
use close() and that the most likely reason is that this is because it is
provided by his implementation.
I'm pretty sure I've created a function called close() that
didn't match the Posix spec. (I'll not argue it was a
really good idea)

--
Nick Keighley
Jun 27 '08 #17
On 16 May 2008 at 10:25, CBFalconer wrote:
Of course the Twink-troll also snipped my explanation, which
follows below:
Indeed I did. Because it was utter bullshit. It was an expression of
your opinion, which I violently disagree with, and I don't think it is
worthy of a reply. You clearly never get bored of endless rants about
topicality, but I'm no longer rising to the bait.

Watch: I'm snipping it again.

[snip]
which is part of the trolls standard mechanism, aiming to disturb
the normal operation of the newsgroup.
Of course, replying to spam doesn't disturb the normal operation of the
newsgroup in the slightest. Neither does your continual attempts to
drive away new participants in the group by continually spewing out your
tedious views on topicality. And neither does the stream of
embarrassingly wrong answers you provide to questions.
I see no reason to ever use open, close and read, when fopen, fclose
and fread are available everywhere, and portable.
Of course you don't. You have no interest in any program more
complicated than a solution to one of the exercises in K&R. You have an
arrogant disdain for people who dirty their hands with real-world
programming, when they might come across good reasons to use open, close
and read. Here are the first few that spring to mind:

* to be able to work with file permissions and ownership
* getting file information with fstat (e.g. the size of a file)
* locking files with fcntl or flock
* getting file change notifications with fcntl
* to be able to send ioctls to devices

Jun 27 '08 #18
In article <sl*******************@nospam.invalid>,
Antoninus Twink <no****@nospam.invalidwrote:
>On 16 May 2008 at 10:25, CBFalconer wrote:
>I see no reason to ever use open, close and read, when fopen, fclose
and fread are available everywhere, and portable.
>Here are the first few that spring to mind:
>* to be able to work with file permissions and ownership
* getting file information with fstat (e.g. the size of a file)
* locking files with fcntl or flock
* getting file change notifications with fcntl
* to be able to send ioctls to devices
Those all involve extensions beyond the perview of C itself, and
are best discussed in a newsgroup that deals with the scope of the
extensions required (e.g., POSIX is going to have different behaviours
than Linux or MS Windows.)

In every extension I can currently think of that supports
the operations listed, open(), close() and read() are unnecessary
for any of the listed operations: those operations work on file
descriptors, and one can determine a FILE's file descriptor using
the fileno() extension upon a file that one has fopen()'d.
And if there are OS's that support file descriptors but do not
offer fileno() but still offer the listed operations, then such
OSs would certainly not be operating in any standardized way, which
would make it even more important to discuss the details in
an appropriate newsgroup rather than here.

--
"This quitting thing, it's a hard habit to break once you start."
-- Walter Matthau
Jun 27 '08 #19
On 16 May 2008 at 18:00, Walter Roberson wrote:
In every extension I can currently think of that supports
the operations listed, open(), close() and read() are unnecessary
for any of the listed operations: those operations work on file
descriptors, and one can determine a FILE's file descriptor using
the fileno() extension upon a file that one has fopen()'d.
Are you saying that to create a file with 640 permissions you would use
fopen+fileno+fcntl rather than just open()?

Jun 27 '08 #20
In article <sl*******************@nospam.invalid>,
Antoninus Twink <no****@nospam.invalidwrote:
>On 16 May 2008 at 18:00, Walter Roberson wrote:
>In every extension I can currently think of that supports
the operations listed, open(), close() and read() are unnecessary
for any of the listed operations: those operations work on file
descriptors, and one can determine a FILE's file descriptor using
the fileno() extension upon a file that one has fopen()'d.
>Are you saying that to create a file with 640 permissions you would use
fopen+fileno+fcntl rather than just open()?
fopen() followed by the fchmod() extension would do fine in situations
where race conditions were not an issue. (Your listed operation
was "work with" permissions, not the more difficult task of
handling files securely.)
--
"Pray do not take the pains / To set me right. /
In vain my faults ye quote; / I wrote as others wrote /
On Sunium's hight." -- Walter Savage Landor
Jun 27 '08 #21
Walter Roberson wrote:
In article <sl*******************@nospam.invalid>,
Antoninus Twink <no****@nospam.invalidwrote:
>On 16 May 2008 at 10:25, CBFalconer wrote:
>>I see no reason to ever use open, close and read, when fopen, fclose
and fread are available everywhere, and portable.
>Here are the first few that spring to mind:
>* to be able to work with file permissions and ownership
* getting file information with fstat (e.g. the size of a file)
* locking files with fcntl or flock
* getting file change notifications with fcntl
* to be able to send ioctls to devices

Those all involve extensions beyond the perview of C itself, and
are best discussed in a newsgroup that deals with the scope of the
extensions required (e.g., POSIX is going to have different behaviours
than Linux or MS Windows.)

In every extension I can currently think of that supports
the operations listed, open(), close() and read() are unnecessary
for any of the listed operations: those operations work on file
descriptors, and one can determine a FILE's file descriptor using
the fileno() extension upon a file that one has fopen()'d.
And if there are OS's that support file descriptors but do not
offer fileno() but still offer the listed operations, then such
OSs would certainly not be operating in any standardized way, which
would make it even more important to discuss the details in
an appropriate newsgroup rather than here.

You snipped one part of the answer of Mr Twink:

You have no interest in any program more
complicated than a solution to one of the exercises in K&R. You have an
arrogant disdain for people who dirty their hands with real-world
programming, when they might come across good reasons to use open, close
and read.

Why would you need to use fileno each time when you just
use open/close etc and be done with it?

You gave no valid reason.
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Jun 27 '08 #22
jacob navia <ja***@nospam.orgwrites:
[...]
Why would you need to use fileno each time when you just
use open/close etc and be done with it?

You gave no valid reason.
I'm sure there are plenty of valid reasons to use open/close rather
than fileno.

comp.unix.programmer would be a great place to discuss those reasons
(if they're not already thoroughly covered in the FAQ).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #23

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Waitman Gobble | last post by:
Hello, I am new to Python. I am having trouble with zipfile.py. On a Linux machine with python 2.4.2 I have trouble opening a zipfile. Python is complaining about the bit where it does a...
0
by: Felix Finch | last post by:
I have a perl test program which has about 80 test cases, each of which creates its own schema so I can remove them with DROP SCHEMA xxx CASCADE. Normally each test case creates and drops the same...
59
by: Rico | last post by:
Hello, I have an application that I'm converting to Access 2003 and SQL Server 2005 Express. The application uses extensive use of DAO and the SEEK method on indexes. I'm having an issue when...
4
by: Vlad | last post by:
I am having problems using the file.create method within a function that is called when looping through an array of filepaths. If I call my function with a hardcoded file path --C:\Temp.txt the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.