Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

CreateFileMapping

weixj2003ldweixj2003ld Posts: 11Member
//qwFileSize=200G;
whFile = CreateFile( tmpfilename.utf16() , GENERIC_WRITE | GENERIC_READ, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,NULL);
if ( whFile == INVALID_HANDLE_VALUE )
{
QMessageBox::warning( 0, "wCreateFile", QString::fromLocal8Bit("fails to create map file") );
return false;
}
whMapFile= CreateFileMapping(whFile,NULL, PAGE_READWRITE,(DWORD)(qwFileSize>>32), (DWORD)(qwFileSize&0xFFFFFFFF),NULL);
if ( whMapFile==NULL )
{
QMessageBox::warning(0,"wCreateFileMapping",QString::number(GetLastError()));
QMessageBox::warning( 0, "wCreateFileMapping", QString::fromLocal8Bit("fails to create map file") );
return false;
}


Error:
CreateFileMapping:GetLastError return 112,and I find that it means "has not enough disk space"
how to deal with it?
Thks in advance.
«1

Comments

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]Try the following (see RED):[/color]
    [code]
    : //qwFileSize=200G;
    : whFile = CreateFile( tmpfilename.utf16() , GENERIC_WRITE |
    : GENERIC_READ, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,NULL);
    : if ( whFile == INVALID_HANDLE_VALUE )
    : {
    : QMessageBox::warning( 0, "wCreateFile",
    : QString::fromLocal8Bit("fails to create map file") );
    : return false;
    : }
    : whMapFile= CreateFileMapping(whFile,NULL,
    : PAGE_READWRITE[color=Red]|SEC_RESERVE[/color],(DWORD)(qwFileSize>>32),
    : (DWORD)(qwFileSize&0xFFFFFFFF),NULL);
    : if ( whMapFile==NULL )
    : {
    :
    : QMessageBox::warning(0,"wCreateFileMapping",QString::number(GetLastEr
    : ror()));
    : QMessageBox::warning( 0, "wCreateFileMapping",
    : QString::fromLocal8Bit("fails to create map file") );
    : return false;
    : }
    : [/code]
    :
  • weixj2003ldweixj2003ld Posts: 11Member
    Thk u.
    but problems still exist.
  • weixj2003ldweixj2003ld Posts: 11Member
    Now I will re-ask my question as follows(I am sorry for poor English):

    I want to write a 55022150160Byte file,and use CreateFileMapping and MapViewOfFile.

    I think I must allocate 55022150160Byte space to it,If I allocate less space,I do not know how to rellocate space to the written file.

    that is,

    Qint64 qwFileOffset=55022150160;

    whMapFile= CreateFileMapping(whFile,NULL, PAGE_READWRITE|SEC_RESERVE,(DWORD)(qwFileSize>>32), (DWORD)(qwFileSize&0xFFFFFFFF),NULL);

    if ( whMapFile==NULL )
    {
    QMessageBox::warning(0,"wCreateFileMapping",QString::number(GetLastError()));
    QMessageBox::warning( 0, "wCreateFileMapping", QString::fromLocal8Bit("fails to create map file") );
    return false;
    }

    and then use following code to map the file to the process with different segmentations.

    wlpwFileAddr=(BYTE *)MapViewOfFile(whMapFile,FILE_MAP_WRITE|FILE_MAP_READ, (DWORD)(wqwFileOffset>>32), (DWORD)(wqwFileOffset&0xFFFFFFFF),offset);

    but when error occur:

    GetLastError()=112,I find that it means "has not enough disk space."

    and my C disk has 48984297472 free space,and less than 55022150160.





  • alexsmth114alexsmth114 Posts: 7Member
    Very good procedure posted, thanks a lot!!..
  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]I do not understand your problem. You need a 55Gb file, but system tells you that there is no disk room for that and you REALLY has no disk room - only 48Gb. The error code is proper, I think.

    Other question I would have asked is that if you need that much memory - then something is wrong with your algorithm. It is ALWAYS the case! When someone tries to create thousands of HWNDs or threads or other excessive thing - then I must ask why? Because, usually, the application does not need that much - there are other ways - ALWAYS![/color]
  • weixj2003ldweixj2003ld Posts: 11Member
    Thk u very much.
    I try to answer what you ask me.
    1.why I need to write so large file?
    I have another 55G file,and I need extract part of its data to save the new file(its size must be less than 55G,but I don't know what size of it precisely in advance,maybe 53G,54G,etc),Maybe you ask me why you need so large file,you can split it into many small files,I think,55Gb file,in petroleum domain,is not large file.
    2.In order to speed up the speed of extracting data and writing data(to create the 55Gb file),I use "file mapping object" in windows,which is involved in functions,as you know,such as CreateFileMapping and MapViewOfFile.
    I open the 55G file and create map object of it with the following code:
    hFile = CreateFile( fileName.utf16() , GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
    if ( hFile == INVALID_HANDLE_VALUE )
    {
    QMessageBox::warning( 0, "aaa1", QString::fromLocal8Bit("reading data fail 1") );
    return false;
    }
    hMapFile= CreateFileMapping(hFile,NULL, PAGE_READONLY,(DWORD)0,(DWORD)0,NULL);
    if ( hMapFile==NULL )
    {
    QMessageBox::warning( 0, "aaa2", QString::fromLocal8Bit("reading data fail 2") );
    return false;
    }


    I create the object file with following code:
    qwFileSize=54G;//[color=Red]define the size of writing file,its size is less than 55G,here I define it 54G;[/color]
    QString Objfilename="h:\tmp.sgy";//[color=Red]here H disk has 487G free space[/color].
    ObjhFile = CreateFile( Objfilename.utf16() , GENERIC_WRITE | GENERIC_READ, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,NULL);
    if (ObjhFile == INVALID_HANDLE_VALUE )
    {
    QMessageBox::warning(0,"",QString::number(GetLastError()));
    QMessageBox::warning( 0, "wCreateFile", QString::fromLocal8Bit("writing data fail 1") );
    return false;
    }
    ObjhMapFile= CreateFileMapping(ObjhFile,NULL, PAGE_READWRITE,(DWORD)(qwFileSize>>32), (DWORD)(qwFileSize&0xFFFFFFFF),NULL);
    if ( objhMapFile==NULL )
    {
    QMessageBox::warning(0,"wCreateFileMapping",QString::number(GetLastError()));//[color=Red]Here occur error: 112,means that the disk has not enough space.[/color]
    QMessageBox::warning( 0, "wCreateFileMapping", QString::fromLocal8Bit("writing data fail 2") );
    return false;
    }

    the H disk has more than 55G(487G) free space,the error still exist.I think that,the function CreateFileMapping does not use the disk the object file will be save to,but use the default disk(maybe C:).
    if I want to create a large file,how to deal with it when c disk has not enough free space?
    3.I want to extract part of data from a large file,how to accelerate the process of it except "file mapping".Some one tell me create a file index,I do not know how to realize it.
    4.Thk u a lot for help me.
  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]If you look closely at your previous post(s) - you will see that you mentioned value of 48984297472 for a disk size and it is NOT 487 Gb - it is only ~48 Gb.

    So, basically, you have a 55Gb INPUT file and you need to process it - to read it, I assume - and create a 55Gb or less OUTPUT file. Answer is very simple - create a buffer of, say 8Mb (larger size will not make it faster - because of HD limits) - using VirtualAlloc() and read the input data into it by simple ReadFile() function with a handle opened in NO_BUFFERING mode - it will speed up the reading. Then simply process file by pieces of 8Mb and again write it into output buffer of another 8Mb (16Mb of memory you need in total here - nothing for Win32 basically...). When output buffer is full - dump it to disk using same NO_BUFFERING method. Very simple and fast code. You only need enough room on disk.
    [/color]
  • weixj2003ldweixj2003ld Posts: 11Member
    Thk u very very much.
    I will try it.

    By the way,the first post I submit, about ~48G is my C: disk,487G is my H: disk,whatever,I will try your suggestion.
  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]We can go a little further -- for example: why to re-write the huge file? I can see how this might be a MUST DO situation, when you need to remove some sensitive data from the file before maybe sending it to someone else. Then you do need to re-write it.

    However, if you trying to delete some information from the file just for yourself and output file really do not need to be sent. Then you might just mark data in the file as removed WITHOUT actually removing it. It does not work well with text files, but very fast with binary files, where each record in a file has same size.

    So, you see -- not always a direct approach is best - you can find other options to make it more effective. Re-writing 55Gb file will take some visible time.[/color]
  • weixj2003ldweixj2003ld Posts: 11Member
    Thk u for your help.
    1. I have test the 'VirtualAlloc' and 'FILE_FLAG_NO_BUFFERING' with the following code(part)
    hFile = CreateFile( fileName.utf16() , GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_NO_BUFFERING,NULL);
    if ( hFile == INVALID_HANDLE_VALUE )
    {
    ...
    }whFile = CreateFile( wfileName.utf16() , GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_NO_BUFFERING,NULL);
    if ( whFile == INVALID_HANDLE_VALUE )
    {
    ...
    }

    int nWriteSize=8*1024*1024;
    int nReadSize=8*1024*1024;

    PBYTE writeBuffer = (PBYTE) VirtualAlloc(NULL,nWriteSize,MEM_COMMIT,PAGE_READWRITE);


    PBYTE readBuffer = (PBYTE) VirtualAlloc(NULL,nReadSize,MEM_COMMIT,PAGE_READWRITE);
    while(offset<filesize)
    {
    ReadFile(hFile,readBuffer,nReadSize,&lpNumberOfBytesRead,NULL);
    memcpy(...);
    WriteFile(whFile, writeBuffer, nWriteSize, &lpNumberOfBytesWrite, NULL )
    }
    ....

    the size of my test data is 795,374,316 byte,and it cost 15s to finish the work.
    2.If I use CreateFileMapping,MapViewOfFile to extract data and write into a new file with my test data(795,374,316 byte),it cost 13s.
    But as you know,if the size of data file is large(for example about 55G),the 112(by getLastError) is returned of CreateFileMapping.(The error I have mentioned in my first post).I do not know how to deal with it.
    3.You said that 'just mark data ',and I find that the data(SEGY format,maybe you know) has no space I can mark.

    Thk u.



«1
Sign In or Register to comment.