R
Rob Kreger
This code works on Windows XP with VC7.1 stl(sxl 3.08 from
dinkumware.)
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <io.h>
#include <fstream>
#include <sys/locking.h>
int _tmain(int argc, _TCHAR* argv[])
{
FILE* lFile = fopen("MyFile","w");
int lfd = _fileno( lFile );
while(1)
{
int lReturn = _locking(lfd,LK_NBLCK,10000);
if(lReturn != -1)
break;
if(errno != EACCES )
throw("Lock failure");
}
std::filebuf lfilebuf(lFile);
std:stream lostream(&lfilebuf);
lostream << "some text" << std::endl;
_locking(lfd,LK_UNLCK,10000);
fclose(lFile);
return 0;
}
Is there a better to way to lock a file with c++?
How can I implement a read and write lock for the same file?
The problem I have with this code is it is not portable? Is there a
portable solution?
I have many objects that overload << and I need to use a stream.
Why doesn't the ansi c++ standard address this issue, the
functionality, of protecting files, seem very trivial to me!!!
Any help would be greatly appreciated.
Rob Kreger
dinkumware.)
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <io.h>
#include <fstream>
#include <sys/locking.h>
int _tmain(int argc, _TCHAR* argv[])
{
FILE* lFile = fopen("MyFile","w");
int lfd = _fileno( lFile );
while(1)
{
int lReturn = _locking(lfd,LK_NBLCK,10000);
if(lReturn != -1)
break;
if(errno != EACCES )
throw("Lock failure");
}
std::filebuf lfilebuf(lFile);
std:stream lostream(&lfilebuf);
lostream << "some text" << std::endl;
_locking(lfd,LK_UNLCK,10000);
fclose(lFile);
return 0;
}
Is there a better to way to lock a file with c++?
How can I implement a read and write lock for the same file?
The problem I have with this code is it is not portable? Is there a
portable solution?
I have many objects that overload << and I need to use a stream.
Why doesn't the ansi c++ standard address this issue, the
functionality, of protecting files, seem very trivial to me!!!
Any help would be greatly appreciated.
Rob Kreger