LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Iseek c or c++ (https://www.linuxquestions.org/questions/linux-newbie-8/iseek-c-or-c-4175448293/)

ofer4 02-02-2013 01:38 AM

Iseek c or c++
 
hi

i forgot to mention that i execute this code in unix

how iseek function work?

for example, if i execute this code:

read(fd,BUFFER,1024)
ISEEK(fd,1024,SEEK_SET)
read(fd,BUFFER,1024)
ISEEK(fd,1024,SEEK_SET)
read(fd,BUFFER,1024)

which byte i will read in the second read func?

the start byte will be 1024? or the start byte will be 2048?


and also in the third read func: i will read the 2048 byte? or 3056 byte or the 1024 byte?



thanks alot

jpollard 02-02-2013 05:21 AM

Assuming ISEEK is a macro invoking lseek, using SEEK_SET sets the pointer to 1024 (0 offset), so byte 1024 will be the one read in both cases.

ofer4 02-02-2013 05:38 AM

ok thanks you!

ofer4 02-02-2013 09:20 AM

hi

there is something that i still dont sure about it:

the SEEK_SET flag is takes me to the absolute offset relativly from the start of of the file? or relatively from the last iseek? or relatively from the last read?

can someone give me detal explanation?

i will be happy for examples too

thanks alot!

jpollard 02-02-2013 09:31 AM

From the manpage on lseek:

Code:

SEEK_SET
      The offset is set to offset bytes.

SEEK_CUR
      The offset is set to its current location plus offset bytes.

SEEK_END
      The offset is set to the size of the file plus offset bytes.

The lseek() function allows the file offset to be set beyond the end of
the file (but this does not change the size of the file).  If  data  is
later written at this point, subsequent reads of the data in the gap (a
"hole") return null bytes ('\0') until data is  actually  written  into
the gap.


johnsfine 02-02-2013 11:17 AM

Quote:

Originally Posted by ofer4 (Post 4883024)
the SEEK_SET flag is takes me to the absolute offset relativly from the start of of the file?

Yes.

Quote:

or relatively from the last iseek? or relatively from the last read?
No.

Quote:

Originally Posted by jpollard (Post 4883030)
From the manpage on lseek:
Code:

SEEK_SET
      The offset is set to offset bytes.


You and I know what that means, but it could look ambiguous to anyone asking the question ofer4 asked.

What does the word offset refer to (just the one I marked in red, not the other)?

If you don't understand that use of offset is implicitly relative to the start of the file, then that man page quote doesn't really answer ofer4's question.

Quote:

Originally Posted by jpollard (Post 4882908)
Assuming ISEEK is a macro invoking lseek

You lost me on where that assumption comes from. After a quick google search, I still have no idea what ISEEK is. But if it really is equivalent to lseek, then the rest of what has been said here makes sense.

Quote:

Originally Posted by ofer4 (Post 4882836)
Iseek c or c++
...
how iseek function work?
...
ISEEK(fd,1024,SEEK_SET)

Iseek is written three different ways in the first post. So maybe it is just an error and should have been lseek.

In some fonts it might be easy to misread lseek as Iseek, then if you are careless about upper/lower case you might get iseek or ISEEK.

ofer4 02-02-2013 12:30 PM

thanks alot you both!

also, i am sorry, i meant "lseek"

thanks alot johnsfine for the detailed explanation!


All times are GMT -5. The time now is 12:57 AM.