Thread: how to open ofstream in exclusive mode

  1. #1
    Registered User
    Join Date
    Aug 2005

    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.
    #include <fstream>
    #include <iostream>
    using namespace std;
    int main(int argc, char* argv[])
    	ofstream out("hello.txt",ios::app);
    		cout << "File is open.. press <Enter> to continue ...";
    		out << "Hello World" << endl;
    		cout << "File failed to open.. press <Enter> to continue ...";
    	return 0;

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Out of scope
    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>:

    The protection mode of the file
    Compatibility share mode (MS-DOS only).
    Exclusive mode no sharing.

    Read sharing allowed.
    Write sharing allowed.

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

  3. #3
    Registered User
    Join Date
    Aug 2005
    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
    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

  5. #5
    Registered User
    Join Date
    Aug 2005
    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.)

    #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>");
    		printf("File open failed --press <Enter>");
       return 0;  /* indicates successful program execution */
    Last edited by Ancient Dragon; 12-01-2005 at 09: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, 09:48 PM
  3. Open Source Licenses
    By Mario F. in forum A Brief History of
    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