🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
4 messages
4 total messages Started by Ray Sat, 10 May 2003 14:01
Help with Mapping a file into memory
#16487
Author: Ray
Date: Sat, 10 May 2003 14:01
18 lines
1582 bytes
Hi All

I wish to map a simple data file (ascii text file in essence) into memory in
my application so that subsequent calls to that file use the data in memory
instead of reopening the file. I presume that this will be faster for large
files (although Windows file caching may mean that I am mistaken about
this).

Have had a look at CreateFileMapping() and MapViewofFile() in the API Help
but I find them a little confusing especially as there are no examples that
show the usage of the data in the way I want.

Can anyone point me to a source of more information and or examples, or
could anyone kindly give an example of what I am trying to achieve.

Best Regards

Ray
Re: Help with Mapping a file into memory
#16488
Author: Sam Mildener
Date: Sat, 10 May 2003 16:11
22 lines
2061 bytes
Ray wrote:
> Hi All
>
> I wish to map a simple data file (ascii text file in essence) into
> memory in my application so that subsequent calls to that file use
> the data in memory instead of reopening the file. I presume that this
> will be faster for large files (although Windows file caching may
> mean that I am mistaken about this).
>
> Have had a look at CreateFileMapping() and MapViewofFile() in the API
> Help but I find them a little confusing especially as there are no
> examples that show the usage of the data in the way I want.
>
> Can anyone point me to a source of more information and or examples,
> or could anyone kindly give an example of what I am trying to achieve.

Here's an example of a class that wraps up all the code:
http://www.microsoft.com/msj/0498/hoodtextfigs.htm#fig4

--

Sam Mildener
Re: Help with Mapping a file into memory
#16494
Author: Remy Lebeau (TeamB)
Date: Sun, 11 May 2003 05:56
41 lines
2047 bytes
"Ray" <raymondlewis@beeb.net> wrote in message
news:3ebd068b@newsgroups.borland.com...

> Have had a look at CreateFileMapping() and
> MapViewofFile() in the API Help

Those are the correct functions to use, yes.

> I find them a little confusing especially as there are
> no examples that show the usage of the data in the
> way I want.

Here you go:

    HANDLE hFile = CreateFile(...);
    if( hFile != INVALID_HANDLE_VALUE )
    {
        HANDLE hMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0,
0, NULL);
        if( hMapping != NULL )
        {
            LPBYTE lpData = (LPBYTE) MapViewOfFile(hMapping, FILE_MAP_READ,
0, 0, 0);
            if( lpData != NULL )
            {
                // use lpData as needed, not to exceed the size of
                // the file.  You can use can use GetFileSize() if you
                // need to determine the size of the file ahead of time

                UnmapViewOfFile(lpData);
            }

            CloseHandle(hMapping);
        }

        CloseHandle(hFile);
    }


Gambit
Re: Help with Mapping a file into memory
#16528
Author: Ray
Date: Mon, 12 May 2003 17:46
3 lines
981 bytes
Thanks Remy works a treat

Ray
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads