Solved

offset while reading file

Posted on 2004-09-03
6
275 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
  • 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
rhel6 C system() call to zip to archive has problems 25 236
UPD maximums on Red Hat 6 109
How to jump to matching brace in eclipse editor ? 1 264
outlook office 365 8 81
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 pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

914 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

19 Experts available now in Live!

Get 1:1 Help Now