[sclug] C++ Quickie

Paul Vanlint paul at polyzing.com
Wed Dec 10 11:46:20 UTC 2003


Hi Tim,

fpos_t is not always a simple integer. I think for GNU it used to be in the
past, but now it is a structure that also includes some state information. I
think you should treat it as a hidden, internal structure that may change in
the future, or across platforms.

You can use fseek and ftell and then you can use an integer offset, however,
the manual seems to recommended that you use fgetpos and fsetpos to ensure
that file repositioning works on different platforms.

Does the bookmark file need to be humanly readable?

If not, then the best thing may be to simply write the fpos_t structure out
as a binary block, so that you can then read it back in as a binary block.
In that way, you avoid any kind of conversion. Although, this may not work
if you want the bookmark file to be portable between different platforms
too.

I hope this helps.

Regards,

Paul.




> -----Original Message-----
> From: sclug-bounces at sclug.org.uk [mailto:sclug-bounces at sclug.org.uk]On
> Behalf Of Tim Sutton
> Sent: Wednesday, December 10, 2003 10:52 AM
> To: sclug at sclug.org.uk
> Subject: [sclug] C++ Quickie
>
>
> Hi All
>
> Just a quickie- hopefully someone wont mind helping out ?
>
> I am trying to persist the place where a file is open (the 'data' file).
> Basically I am getting an fpos_t of the current place that I am
> in the data
> file and writing it to another file (my 'bookmark' file). When I read the
> position back from the bookmark  file and want to restore my
> position in the
>
> data file, fscan it from the bookmark file and get it as an int. How do I
> convert the int back to an fpos_t so that I can use it to set the filepos?
>
> The above is a bit of a simplification of what I am actually
> trying to do -
> which is to build a vector of all the bookmarks in the file as
> shown in the
> snippet below.
>
> Basically it is the static_cast<fpos_t>(&myCurrentMarkerInt) which doesnt
> work
> and I am trying to figure out what I should replace that with.
>
>
>     else  //we managed to open the blockmarkers file so just read them in
> from
> there
>     {
>     cout << "FileReader::getBlockMarkers reading markers from cache file "
> <<
> endl;
>       int myCurrentMarkerInt=0;
>       do
>       {
>         fscanf (myBlockMarkersFilePointer, "%i", &myCurrentMarkerInt);
>
> dataBlockMarkersVector.push_back(static_cast<fpos_t>(&myCurrentMar
> kerInt));
>       }    while (!feof(myBlockMarkersFilePointer)) ;
>
>        fclose(myBlockMarkersFilePointer);
>        return &dataBlockMarkersVector;
>     }
>
> Many Thanks
>
> Tim
>
> Tim Sutton
> BDWorld Middleware Programmer
> - -------------------------------------------------------------------
> BiodiversityWorld Project
> Centre for Plant Diversity & Systematics
> School of Plant Sciences
> The University of Reading
> Reading, RG66AS, UK
>
> Web     :   www.bdworld.org
> Phone  :   +44-(0)118-378-6052
> Email    :   t.sutton [at] reading.ac.uk
> (email preferred method of correspondence)
> - -------------------------------------------------------------------
> _______________________________________________
> sclug mailing list
> sclug at sclug.org.uk
> http://www.sclug.org.uk/mailman/listinfo/sclug
>



More information about the Sclug mailing list