473,785 Members | 2,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,b uf);
perror("READ");
close(fd);
perror("CLOSE") ;
}
}
Jun 27 '08 #1
22 7273
In article <05************ *************** *******@p25g200 0pri.googlegrou ps.com>,
smarty <cs*******@gmai l.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************ *************** *******@a70g200 0hsh.googlegrou ps.com>,
Szabolcs Borsanyi <bo******@thphy s.uni-heidelberg.dewr ote:
>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************ *************** *******@p25g200 0pri.googlegrou ps.com>,
smarty <cs*******@gmai l.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,b uf);
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********@mai neline.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*******@gmai l.comschrieb im Newsbeitrag
news:05******** *************** ***********@p25 g2000pri.google groups.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,b uf);
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********@yah oo.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,b uf);
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_Keit h) 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********@mai neline.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

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

Similar topics

1
4217
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 seek(-22,2). Looks to me like zipfile.py is trying to come back 22 bytes from the end of file. # python
0
314
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 schema, but it can run a mode to preserve each schema and all the disk files for each test. I recently changed my cleanup code to run psql with all -c commands on one command line rather than a separate psql -c for each one ... psql -c 'DROP...
59
7523
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 the recordset opens a table. When I write Set rst = db.OpenRecordset("MyTable",dbOpenTable, dbReadOnly) I get an error. I believe it's invalid operation or invalid parameter, I'm
4
6916
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 function creates the file as expected. When I loop through my array I get the error - "ArgumentException was unhandled - Illegal characters in path" The value "C:\Temp.txt" is the first value in the array - as it works
0
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10319
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10087
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9947
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7496
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.