Skip to content

Instantly share code, notes, and snippets.

@qrealka
Created April 25, 2018 15:04
Show Gist options
  • Save qrealka/22add91db3772f2c9457459b524f6ebe to your computer and use it in GitHub Desktop.
Save qrealka/22add91db3772f2c9457459b524f6ebe to your computer and use it in GitHub Desktop.
shared_file_access
// shared_file.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <Windows.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/locking.h>
#include <share.h>
#include <fcntl.h>
#include <cstdio>
#include <cstdlib>
#include <io.h>
#include <safeint.h>
#include <cstdint>
namespace
{
char buffer[] = "This is a test of '_write' function\n";
}
int _tmain(int argc, _TCHAR* argv[])
{
int file;
unsigned bytesWritten = 0;
const auto err = _sopen_s(&file, "d:\\work\\shared.txt", _O_WRONLY | _O_CREAT | _O_APPEND, _SH_DENYNO, _S_IREAD | _S_IWRITE);
printf("%d %d\n", err, file);
if (err != 0)
exit(1);
for (int i = 1; i < 1000000; ++i)
{
if ((bytesWritten = _write(file, buffer, sizeof(buffer) - 1)) == -1)
{
printf_s("Unexpected error %d\n", errno);
break;
}
else
{
printf_s("Wrote %u bytes to file.\n", bytesWritten);
::Sleep(50);
}
}
_close(file);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment