CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2001
    Posts
    51

    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?

  2. #2
    Join Date
    May 2004
    Posts
    15
    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;
    }

  3. #3
    Join Date
    Aug 2001
    Posts
    51
    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.

  4. #4
    Join Date
    Aug 2001
    Posts
    51
    hmmm... the include didn't come out, but it should be there

  5. #5
    Join Date
    Jul 2004
    Posts
    1
    >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.

  6. #6
    Join Date
    Aug 2001
    Posts
    51
    That wouldn't compile either:

    error C2039: 'sh_none' : is not a member of 'std::basic_filebuf&lt;_Elem,_Traits&gt;'

    Here's a simple example --

    Code:
    #include "stdafx.h"
    #include &lt;fstream&gt;
    #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
  •  





Click Here to Expand Forum to Full Width

Featured