Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how is seek_set implemented ?

Posted on 2011-09-30
8
Medium Priority
?
522 Views
Last Modified: 2012-05-12
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
Comment
Question by:zizi21
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 9

Expert Comment

by:user_n
ID: 36895720
0
 
LVL 9

Assisted Solution

by:user_n
user_n earned 400 total points
ID: 36895721
0
 

Author Comment

by:zizi21
ID: 36895751
Does this mean that fseek is a linux implementation and not a C implementation ? Thanks.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:zizi21
ID: 36895766
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
 
LVL 53

Accepted Solution

by:
Infinity08 earned 1600 total points
ID: 36895795
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
 
LVL 9

Expert Comment

by:user_n
ID: 36895964
0
 

Author Comment

by:zizi21
ID: 36896262
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
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 1600 total points
ID: 36896680
>> 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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question