Solved

how is seek_set implemented ?

Posted on 2011-09-30
8
504 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
  • 3
  • 3
  • 2
8 Comments
 
LVL 9

Expert Comment

by:user_n
Comment Utility
0
 
LVL 9

Assisted Solution

by:user_n
user_n earned 100 total points
Comment Utility
0
 

Author Comment

by:zizi21
Comment Utility
Does this mean that fseek is a linux implementation and not a C implementation ? Thanks.
0
 

Author Comment

by:zizi21
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 53

Accepted Solution

by:
Infinity08 earned 400 total points
Comment Utility
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
Comment Utility
0
 

Author Comment

by:zizi21
Comment Utility
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
Comment Utility
>> 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now