CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2004
    Location
    Pakistan
    Posts
    466

    Question Writing File using CreateFileMapping - Please help

    Hello gurus

    Can anyone please provide be a complete example of writing large files fast using File Maping functions ?

    Following code cause application level crash


    Code:
    char *out = "newfile.txt";
    HANDLE fh_infile = CreateFile(out, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    	if(fh_infile==NULL) return false;
    
    HANDLE h = CreateFileMapping(fh_infile, NULL, PAGE_READWRITE, 0, filesize,  NULL);
    char *dat = (char*)MapViewOfFile(h, FILE_MAP_WRITE, 0, 0, filesize);
    
    char *tempdata = "Here is the temporary data";
    CopyMemory(dat, tempdata, strlen(tempdata)+1);
    UnmapViewOfFile(dat);
    CloseHandle(fh_infile);

    Waiting or the kind reply.

    regards
    » Please 'Rate This Post' if it helped (encourage us to help you more)
    » Build GUI in minute using rad c++
    » Free IDE + GUI code generator - screenshot
    » Free WINAPI sourcecode and tutorials

  2. #2
    Join Date
    May 2008
    Posts
    300

    Re: Writing File using CreateFileMapping - Please help

    File mapping is not something to write file fast. It's more or less used to share pages between processes.

    I rarely create file mapping on normal files... but I think the file should be opened with both read and write access since you want the page to be r&w.

    Don't forget to close the file mapping.
    Nope

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