Thread: ios::nocreate and the latest standard

  1. #1
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    ios::nocreate and the latest standard

    ios::nocreate was removed in the latest standard, for some reason. Without it, how do I open a file (for reading) ONLY if it exists? I read in some other post here that the file shouldn't be opened if it doesn't exists, but mine still does.

    Code:
    ifstream ReadFile;
    
    ReadFile.open(FileName, ios::in); //I want ot add ios::nocreate here too...
    if(ReadFile.fail())
    {
       //Never reached, even though the file doesn't exists
       return 0;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Your implementation is noncompliant; a conforming compiler will enter your error handler. Upgrade your compiler, I guess.

    Assuming "notareal.fil" does not exist, the following should print "Failure." on any conforming compiler:

    Code:
    #include <iostream>
    #include <fstream>
    
    int main(){
    
    	std::ifstream ReadFile;
    
    	ReadFile.open("c:\\notareal.fil", std::ios::in);
    	if(ReadFile.fail())
    		std::cout << "Failure." << std::endl;
    	else std::cout << "Success." << std::endl;
    }
    Last edited by Cat; 12-06-2003 at 05:43 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I'm using Visual C/C++ v6.?
    I thought that would be new enough...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I'm using Visual C/C++ v6.?
    v6 is especially poor at complying to any C++ standards
    The latest .net c++ compiler is much better
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Cat's code works as expected in VC 6.0 (it's not that bad).

    gg

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    I think from memory it was that bad and that one was fixed in a service pack. Have you got your v6c installation up to date magos?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981

Popular pages Recent additions subscribe to a feed