Click to See Complete Forum and Search --> : File Streams
Loosejeans
May 20th, 2004, 01:49 AM
Hi All,
i am having this problem (and i looked in many places and cant find anything useful) in ANSI C the function called "fseek" it is used as;
int fseek(FILE *stream,long int offset,int orgin); i dont know how to use the interger "offset"
please exlpain
Bill
Gabriel Fleseriu
May 20th, 2004, 03:48 AM
I find MSDN explains it quite well:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_fseek.asp
yiannakop
May 20th, 2004, 05:03 AM
fseek is used to "move" the file handle, which as u probably now is not a single pointer, and so u can't "move" it like other pointers (ptr++).
1st arg: is the file pointer
2nd & 3rd arguments are integers which are used to specify where will the pointer move to. The 3rd arg is the initial position (look at msdn for more details) of the pointer and the 2nd arg is HOW MANY BYTES will the pointer move from the initial position.
example 1:
fseek(fp,100,SEEK_SET) moves the fp poiner at the 100th byte of the file.
fseek (fp,0,SEEK_END) moves fp at the end of the file.
Loosejeans
May 20th, 2004, 08:23 PM
Thanks but why is 0 the end of the file and if i use one does it put the EOF to the first byte?!?!?
sudhakarm
May 21st, 2004, 01:56 AM
Ok...
fseek (File pointer name, Size of the record, From where)
(1st arg) (2nd arg) (3rd arg)
1st arg : It's ur file pointer name ex : fp
2nd arg : It is the sizeof the record ex :
Assume that u have a structure
struct emp
{
int empno;
char ename[20];
float sal;
}e;
The size of the struct emp e variable is 2+20+4 = 26 bytes.
While specifying the sizeof the record u have to specify the sign also i.e -26 or + 26.
ok why and how ???
This depends on the 3rd argument i.e
a) SEEK_SET : means from the begining of the file. Always u can move to front only, means u have give +26.
b) SEEK_CUR : means from the current position of the file. U can move either front or back, means u can give +26 or -26 according to u requirement.
c) SEEK_END : means from the the end of the file. Always u can move to backwards only, means u have to give -26.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.