Solved

offset while reading file

Posted on 2004-09-03
6
286 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert image to byte array 8 201
My eclipse editor won't start ? 4 154
Trouble linking program with -lcrypt 3 149
UPD maximums on Red Hat 6 115
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
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.

810 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