Seek_end

This is a discussion on Seek_end within the C Programming forums, part of the General Programming Boards category; I want to read a the last record in my structure. I don't know how to do this (I think ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    Seek_end

    I want to read a the last record in my structure. I don't know how to do this (I think you use SEEK_END but I can't work out how to use it) so can some please help me out
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    You need to know what form that last entry will be...a long, a byte another struct!!!

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    umm..

    I don't quite understand what you mean. But this is my sturc
    Code:
    struct data
    {
        char fname [51],sname [51],tel[15];                 
        int x;                                          
    
    }a;
    Is that any help
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Ok...well assuming you mean that the int is the last record, you could address it as
    Code:
    a.x
    IE;
    Code:
    a.x = 10;

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    SEEK_END

    Can someone give me an example of using SEEk_END

    Thank you
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    137
    According to my book seek end is just a condition used with fseek.
    You have either SEEK_SET,SEEK_CUR and SEEK_END. Which have meanings of "seek from start of file", "seek from current location" and "seek from end of file" respectivly.

    int fseek (FILE *stream, long int offset, int origin);
    origin must be one of the above three.
    http://uk.geocities.com/ca_chorltonkids

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I presume you mean you have a file that you wrote with fwrite, and contains a load of structs. Now you want to get the last one in the file back into memory with fread(), right?

    If so, fopen() the file, then fseek() to the correct position something like
    >fseek(fp, sizeof(struct whatever) * -1, SEEK_END);
    then perform the read. This fseek simply seeks a certain amount of bytes backwards from the end of the file.

    Does this help?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21