Thread: fstream open parameters

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

    Question fstream open parameters

    using VC++ .NET 2005 Express Edition on XP. I thought the open function only takes two parameters, but I'm apparently wrong. What is the third parameter? I looked at several fstream tutorials but none of them show a third parameter.

    I came across this problem purely by accident -- the compiler compiled the code without complaint, but the program crashed on that line.
    Code:
    fstream a;
    a.open(filename,ios::in | ios::out , ios::binary | ios::ate);
    The comma in the above line should have been an or operator |.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Open dose indeed only take two paramaters. Make sure your first paramater is an array of chars and not a string object.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Here is the complete program
    Code:
    #include "stdafx.h"
    #include <fstream>
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	char file[255];
    	fstream a;
    	strcpy(file,"mel.txt");
    	a.open(file,ios::in|ios::out, ios::binary|ios::ate);
    	a.close();
    	a.clear();
    	return 0;
    }
    here is stdafx.h
    Code:
    // stdafx.h : include file for standard system include files,
    // or project specific include files that are used frequently, but
    // are changed infrequently
    //
    
    #pragma once
    
    #define WIN32_LEAN_AND_MEAN		// Exclude rarely-used stuff from Windows headers
    #include <stdio.h>
    #include <tchar.h>
    
    
    
    // TODO: reference additional headers your program requires here
    Last edited by Ancient Dragon; 01-26-2006 at 09:45 PM.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    open did in fact have 3 parameters as defined in the heder <fstream.h>
    Code:
            filebuf*        open(const char *, int, int = filebuf::openprot);
    could not find anything in <fstream> but if I see "stdafx.h" in any source I don't wonder much.
    Kurt

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    http://msdn.microsoft.com/library/de....3a3a.open.asp

    Later fixed - http://msdn.microsoft.com/library/de...ementation.asp
    In the new Standard C++ iostream library:
    • open functions do not take a third parameter (the protection parameter).
    [edit]Hmm - iostream, not fstream, but I think it's the same type of thing. And I think MS now uses Dinkumware.
    Last edited by Dave_Sinkula; 01-27-2006 at 09:42 AM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Thanks for the links.

    Quote Originally Posted by MSDN
    open functions do not take a third parameter (the protection parameter).
    Than I wonder why the compiler accepts it? Or whey the open function doesn't just ignore it if its present?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Open Software License 3.0 Explained
    By laserlight in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 01-08-2008, 08:10 PM
  2. open empty file
    By mystic-d in forum C Programming
    Replies: 2
    Last Post: 11-16-2007, 10:27 AM
  3. open files in a loop in C
    By podiyan in forum C Programming
    Replies: 2
    Last Post: 10-19-2007, 01:50 AM
  4. To open or not to open ?
    By darfader in forum C Programming
    Replies: 7
    Last Post: 09-24-2003, 08:14 AM
  5. Problems with open and save boxes
    By pinkcheese in forum Windows Programming
    Replies: 3
    Last Post: 05-21-2002, 06:03 PM