how to open ofstream in exclusive mode

This is a discussion on how to open ofstream in exclusive mode within the C++ Programming forums, part of the General Programming Boards category; using ofstream, how can I open a file in exclusive mode? Someone told me that was the default, but in ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267

    Question how to open ofstream in exclusive mode

    using ofstream, how can I open a file in exclusive mode? Someone told me that was the default, but in my test using VC++ 6.0 on XP that's not the case. I wrote a simple console program (see below) and all instances was able to successfully open and write to the file.
    Code:
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    
    int main(int argc, char* argv[])
    {
    	ofstream out("hello.txt",ios::app);
    	if(out.is_open())
    	{
    		cout << "File is open.. press <Enter> to continue ...";
    		out << "Hello World" << endl;
    		cin.get();
    		out.close();
    	}
    	else
    	{
    		cout << "File failed to open.. press <Enter> to continue ...";
    		cin.get();
    	}
    	return 0;
    }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    I don't really know myself, but in giving it a quick search, I found this link talking about using <fctrl.h> for that:

    C++ Reference Guide

    Although it may not be everyone's cup of tea, C's original I/O library (to which, henceforth, I will refer as <fcntl.h>) is still alive and kicking...
    ... Yet, unofficially, this library is supported on every major platform. One of its major advantages over <fstream> is its tighter control over file I/O operations. For instance, you can use flags that open a file in exclusive mode, or open a file only if it already exists...
    Again, I don't know if this helps, but you could check it out if you wish.

    Aside from that, I also found these arguements to pass the open() command in <fstream>:

    Protection
    The protection mode of the file
    filebuf::sh_compat
    Compatibility share mode (MS-DOS only).
    filebuf::sh_none
    Exclusive mode no sharing.

    filebuf::sh_read
    Read sharing allowed.
    filebuf::sh_write
    Write sharing allowed.

    This may be more what you want. For reference, it's from here.
    Last edited by SlyMaelstrom; 12-01-2005 at 06:44 PM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    unfortunately filebuf::sh_none is only defined in <fstream.h> and not in <fstream>. I agree with your quote that C's file i/o is a lot superior to c++ fstream class. I suppose my only option is to use fsopen() where I can specify the sharing mode. Stupid c++

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    The third argument protection is
    used as the file permissions. It does not appear in the Standard C++
    description and is provided as an extension. It determines the file
    read/write/execute permissions under UNIX. It is more limited under
    DOS since files are always readable and do not have special execute
    permission.
    nmnmnm

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    7stud: MS-Windows function _fsopen() allows and enforces opening a file in exclusive mode (see example below). I'm not running MS-DOS, but XP (which has NO MS-DOS code in it)
    So why doesn't fstream??? (answer: c++ fstream is a crappy horrible class.)

    Code:
    #include <stdio.h>
    #include <share.h>
    
    int main() /* program main begins execution */
    {
    	FILE* fp = _fsopen("myfile.txt","a",_SH_DENYRW);
    	if(fp != NULL)
    	{
    		fprintf(fp,"Hello World\n");
    		printf("File opened ok -- press <Enter>");
    		getchar();
    		fclose(fp);
    	}
    	else
    	{
    		printf("File open failed --press <Enter>");
    	}
    
    
       return 0;  /* indicates successful program execution */
    }
    Last edited by Ancient Dragon; 12-01-2005 at 08:28 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Serial port communcation and computer standby mode
    By MWAAAHAAA in forum Windows Programming
    Replies: 0
    Last Post: 07-09-2009, 10:38 AM
  2. Statistical Mode Function
    By h4rrison.james in forum C Programming
    Replies: 5
    Last Post: 01-16-2009, 08:48 PM
  3. Open Source Licenses
    By Mario F. in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 10-10-2006, 08:53 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21