Solved

how is seek_set implemented ?

Posted on 2011-09-30
8
512 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
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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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

Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

Question has a verified solution.

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

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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 and use structures in the C programming language.
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.

820 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