CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: File Streams

  1. #1
    Join Date
    Mar 2004
    Posts
    31

    File Streams

    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

  2. #2
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    I find MSDN explains it quite well:
    http://msdn.microsoft.com/library/de..._crt_fseek.asp
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  3. #3
    Join Date
    Dec 2001
    Location
    Greece, Athens
    Posts
    1,015
    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.
    Theodore
    Personal Web Page (some audio segmentation tools): www.di.uoa.gr/~tyiannak

  4. #4
    Join Date
    Mar 2004
    Posts
    31
    Thanks but why is 0 the end of the file and if i use one does it put the EOF to the first byte?!?!?

  5. #5
    Join Date
    May 2004
    Posts
    81
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured