Solved

how is seek_set implemented ?

Posted on 2011-09-30
8
516 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 100 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 400 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 400 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

752 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