|
-
May 5th, 2004, 02:12 PM
#1
fstream & file sharing: How to do what you used to be able to do?
With .Net 2003, fstream.h disappeared and the open command went from three parameters to two.
How do I now open a file and deny other applications from accessing the file until I am done writing to it? I used to be able to do:
Code:
fstream outf("somefile.txt",ios::app,filebuf::sh_none);
Is there another way to do this, or am I out of luck?
-
May 9th, 2004, 10:31 PM
#2
the fstream change was one of the hardest changes in .NET for me, as I use a lot of functionality from it.
Try this:
Code:
#include <fstream>
using namespace std;
int main() {
ofstream test //Initiate "test" handle
test.open("somefile.txt",ios::app,filebuf,sh_none); //Open File
test << "Line Written to file"; //Write to file
test.close() //Close handle
return 0;
}
-
June 10th, 2004, 08:54 AM
#3
Code:
#include <fstream>
using namespace std;
int main() {
ofstream test //Initiate "test" handle
test.open("somefile.txt",ios::app,filebuf,sh_none); //Open File
test << "Line Written to file"; //Write to file
test.close() //Close handle
return 0;
}
Are you sure of this code? I get a compile error:
error C2065: 'sh_none' : undeclared
error C2275: 'std::filebuf' : illegal use of this type as an expression.
-
June 10th, 2004, 08:56 AM
#4
hmmm... the include didn't come out, but it should be there
-
July 21st, 2004, 03:37 PM
#5
>Are you sure of this code? I get a compile error:
>error C2065: 'sh_none' : undeclared
>error C2275: 'std::filebuf' : illegal use of this type as an expression.
You should use: filebuf.sh_none if it doesn't accept sh_none.
-
July 22nd, 2004, 12:29 PM
#6
That wouldn't compile either:
error C2039: 'sh_none' : is not a member of 'std::basic_filebuf<_Elem,_Traits>'
Here's a simple example --
Code:
#include "stdafx.h"
#include <fstream>
#include "example.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
using namespace std;
CWinApp theApp;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
nRetCode = 1;
}
else
{
ofstream outf("test.txt",ios::out,filebuf.sh_none);
}
return nRetCode;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|