Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 538
  • Last Modified:

how is seek_set implemented ?

Hi there,

How is seek_set implemented ? Using seek_cur makes the code fast. When I look up seek_set, it says that it begins from position "0" (start of the file).

For instance, if its position is at 140 and I want to go to 150, seek_set would start from the start of file. This means, it would go from 140 to 0. Then, from 0 to 150. Why is seek_set behaving like that? Isn't more effecient to make another 10 steps from 140 to 150 (which i pressume to be the behaviour of seek_cur).

Many thanks.
0
zizi21
Asked:
zizi21
  • 3
  • 3
  • 2
3 Solutions
 
user_nCommented:
0
 
user_nCommented:
0
 
zizi21Author Commented:
Does this mean that fseek is a linux implementation and not a C implementation ? Thanks.
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
zizi21Author Commented:
I mean fseek is a C library implemenation but how is it implemented by linux ? The description of seek_set , seek_cur is what I explained in the question.
0
 
Infinity08Commented:
I'm not sure I understand your question. But I sense some confusion.

SEEK_SET, SEEK_CUR and SEEK_END are just reference points (respectively the beginning of the file, the current position of the file, and the end of the file).

With fseek, you specify which reference point you want to use, and what offset you need respective of that reference point, and then fseek sets the current position to the one indicated by you (assuming it's a valid request).

For example, if you want to go to the 150th character in a binary file, you use SEEK_SET as the reference point, and 150 as the offset :

        fseek(fp, 150, SEEK_SET);

If you know that you are currently at the 140th character, you could achieve the same with :

        fseek(fp, 10, SEEK_CUR);

Which one you use depends entirely on your use case, and which makes more sense.


Note that when you use SEEK_SET, that does not mean that the file will be rewound before going to the target position. The SEEK_SET is merely used to specify the target position. The implementation will take care of getting there in a hopefully efficient way (for sane implementations anyway).
0
 
user_nCommented:
0
 
zizi21Author Commented:
Thanks for the explaination. The thing that is confusing is that I did some code to compare the efficiency of seek_set and seek_cur. The code with seek_set takes a much longer time to execute than seek_cur.
0
 
Infinity08Commented:
>> The code with seek_set takes a much longer time to execute than seek_cur.

Assuming that all other circumstances were the same, then that might mean that the implementation on your platform finds it more convenient to seek from the current position, and needs to go through a bit more trouble to seek from the start of the file.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now