Вопрос

Is it possible to implement transactional file saving in standard C++ without using system specific API calls like CopyFile, ReplaceFile, CreateFile etc.

Currently I am doing it as follows (using Win32 API specific code):

void TransactionalFileSave( const std::wstring& szFileName )
{
      std::wofstream fileTemp( L"temp" );

      for( auto data : vecData )
      {
            fileTemp << data;
      }

      fileTemp.close();

      CloseHandle( CreateFile( szFileName.c_str(), (GENERIC_READ | GENERIC_WRITE), (FILE_SHARE_READ | FILE_SHARE_WRITE), NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL ) );

      CloseHandle( CreateFile( L"temp_backup", GENERIC_ALL, (FILE_SHARE_READ | FILE_SHARE_WRITE), NULL, CREATE_NEW, FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_TEMPORARY, NULL ) );

      ReplaceFile( szFileName.c_str(), L"temp", L"temp_backup", REPLACEFILE_WRITE_THROUGH, NULL, NULL );
}

Is there a way I could achieve a similar thing without using Win32 API specific functions (i.e. just the standard library or boost)?

Это было полезно?

Решение

Short answer is: no.

Long answer: The standard file-functions support only a fairly basic set of functions. If you need to specific functionality like this, that is only supported by some operating systems, then you will need to use system-specific functionality - or find another way to achieve the same result (for example, using rename instead of ReplaceFile)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top