lundi 30 août 2021

Filling up a file of a specific size - How would I be able to accomplish that

I am attempting to create a file using SetFilePointerExto simply set the size of the file. Now I would like to populate that entire file from start to end with the number 1 or any other number. I am not sure how I would accomplish that. Is there a way that I can use to fill up that entire file with a number.

This is the code that I have come up with so far. I now have handle to a file hFile but I am not sure how I can populate it now till the available limit?

  HANDLE hFile = CreateFileA(
        Filename,
        GENERIC_WRITE,
        (FILE_SHARE_READ | FILE_SHARE_WRITE),
        nullptr, 
        OPEN_ALWAYS,
        FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED ,  
        nullptr);  

    if (INVALID_HANDLE_VALUE == hFile)
    {
        std::cout << "Could not create the file";
        return false;
    }
    
    //Set a fixed size
     LARGE_INTEGER li;
     li.QuadPart = uFileSize;

    LARGE_INTEGER liNewFilePointer;

    if (!SetFilePointerEx(hFile, li, &liNewFilePointer, FILE_BEGIN))
    {
        //Could not create the file
        CloseHandle(hFile);
        return false;
    }
    if (liNewFilePointer.QuadPart != li.QuadPart)
    {
        //Error occured
        CloseHandle(hFile);
        return false;
    }

    if (!SetEndOfFile(hFile))
    {
        //Error use GetLastError()
        CloseHandle(hFile);
        return false;
    }

    //We now have a file of a specific size

Aucun commentaire:

Enregistrer un commentaire