Solved

offset while reading file

Posted on 2004-09-03
6
306 Views
Last Modified: 2010-04-15
I am trying to read something from a bitmap and want to read the info at particular offset from the beginning of file. how can i do that?
I tried this but doesn't seem to work:

FILE *f = fopen("filename");
fread(&somestruct, 1, bytes_to_read, f+someOffset);

is there something similar to seekp() (C++)

thx
jay
0
Comment
Question by:jhshukla
[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
6 Comments
 
LVL 2

Accepted Solution

by:
sneeuw_chan earned 25 total points
ID: 11973330
Yup, there is.  It's called fseek().
To be exact: int fseek(FILE *stream, long offset, int whence);
('stream' and 'offset' should be obvious and 'whence' is one of 'SEEK_SET, SEEK_CUR or SEEK_END.)
0
 
LVL 9

Author Comment

by:jhshukla
ID: 11974751
could you explain the meanings of SET, CUR and END? As far as I remember
SET sets the pointer at the offset,
CUR displaces the pointer from the current position by offset bytes
END makes it point at offset bytes (backwards) from the END
0
 
LVL 2

Expert Comment

by:sneeuw_chan
ID: 11974834
Almost.  END makes it point at offset bytes _forwards_ from the end.  Remember, the offset is signed, so it can be negative.
The rest is as you stated, SET is relative to the start of the file, and CUR is relative to the current position.
0
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!

 
LVL 9

Author Comment

by:jhshukla
ID: 11974871
do you mean fseek(somefile, +10, SEEK_END) would make is point to ten bytes beyond the EOF? that is a bad thing!
0
 
LVL 2

Expert Comment

by:sneeuw_chan
ID: 11974922
Yup, 10 bytes beyond the EOF.  Why would that be a bad thing ?
0
 
LVL 9

Author Comment

by:jhshukla
ID: 11974961
not if we know what we are doing. thx.
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 tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

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