std::ifstream, flags

This is a discussion on std::ifstream, flags within the C++ Programming forums, part of the General Programming Boards category; Ok, so this is what I did: 1) include <fstream> 2) create std::ifstream called "file" 3) call file.open(filename, ios::in | ...

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    std::ifstream, flags

    Ok, so this is what I did:
    1) include <fstream>
    2) create std::ifstream called "file"
    3) call file.open(filename, ios::in | ios::binary | ios::nocreate)

    So it says that ios is an invalid namespace or something.

    4) change ios::whatever to std::ios::whatever

    It gets rid of all the errors except for a new one that says that std::ios::nocreate doesn't exist.

    5) include <fstream.h> and change std::ios::whatever to ios::whatever

    It gets rid of all the errors.

    Later, I tried to call file.seekg(position, ios::end). It gives me an error and says that ios::end doesn't exist. I tack on std::, and it works.

    So my question is, is there any problem with mixing <fstream>'s stuff with <fstream.h>'s stuff?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Why don't u just put a using namespace std; after all the include statements? It would simplify things a lot more.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Dunno, I sorta have a thing against doing that But still, is there any incompatibility or anything if I mix the <fstream> stuff with the <fstream.h> stuff?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Originally posted by Hunter2
    Dunno, I sorta have a thing against doing that But still, is there any incompatibility or anything if I mix the <fstream> stuff with the <fstream.h> stuff?
    yes some things are incompatible, a better way is like this
    Code:
    #include <fstream>
    
    using std::ifstream;
    
    int main( )
    {
       ifstream fileOut( "test.txt" );
       fileOut.close( );
       return 0;
    }
    Couldn't think of anything interesting, cool or funny - sorry.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    To answer your question very specifically, 'nocreate' is a Microsoft implementation. Borland, for one, doesn't recognize it and I would be surprised if other compilers did either.

    This is why using 'std::' corrected all but one of your errors using <fstream>. As you found, though, 'nocreate' is non-standard requiring <fstream.h> be included - again, specific to the MSVC++ compiler.

    I don't have a serious problem with MSVC++, and don't intend to get into any debates with those who do have problems with it, or Microsoft, for that matter. That discussion doesn't belong on this Board anyway.

    Just recognize that there will be compatibility issues when you try to mix standard and non-standard headers.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ok, thanks. But is there any standard equivalent of nocreate? Or is there any other way to make sure that the file exists?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537

    this should work

    Code:
    #include <fstream>
    
    using std::ifstream;
    using std::cout;
    
    int main( )
    {
       ifstream fileOut( "test.txt" );
       fileOut.close( );
    
       if( !fileOut )
          cout << "unable to open file\n";
    
       return 0;
    }
    Couldn't think of anything interesting, cool or funny - sorry.

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    You mean it won't create a file even if I don't use ios::nocreate? And fileOut will be NULL? Or could I use if(!fileOut.isOpen())?

    P.S. I'm trying to open it in binary mode, not text mode.

    P.P.S. Why do you call fileOut.close()?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    If it cannot open the file for whatever reason, this normally works. I am not sure why exactly, I think it works using the failbit in the ifstream object, but I'm sure the is_open function will get you pretty much the same results.

    This code works fine (one of the few times i test what I post )

    Code:
       ifstream fileOut( "test.exe", ios::binary | ios::nocreate );
    
       if( !fileOut )
          cout << "unable to open file\n";
    
       fileOut.close( );
    And I always close the file, force of habit really but its a good practice to use and means you can use the fstream object to open a different file if needed.
    Couldn't think of anything interesting, cool or funny - sorry.

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Code:
    int main( )
    {
       ifstream fileOut( "test.txt" );  //create fileOut, open test.txt
       fileOut.close( );  //close test.txt
    
       if( !fileOut )  //if the file is closed,
          cout << "unable to open file\n";  //say "unable to open file"
    
       return 0;
    }
    That's what I meant.

    I think it works using the failbit in the ifstream object
    I doubt it. Check this out:
    If the file is not found, then the ios::failbit is set only if the ios::nocreate mode was used.
    And looky!
    ifstream fileOut( "test.exe", ios::binary | ios::nocreate );
    If you only included <fstream>, ios::nocreate couldn't have worked. You must have included <fstream.h> too! And then fileOut would be either mixing standard/nonstandard or just using nonstandard. And later, if you want to call seekg and use the ios::end flag, there is only a std::ios::end flag, no ios::end flag. Thus, you would be mixing standard and non-standard no matter what Thus, since I need to use ios::end, I have opted against using ios::nocreate in order to keep things standard.
    Last edited by Hunter2; 09-08-2002 at 03:41 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cannot get interface flags: Invalid argument
    By nasim751 in forum C Programming
    Replies: 1
    Last Post: 04-15-2008, 02:27 AM
  2. Cannot get interface flags: Invalid argument
    By nasim751 in forum C Programming
    Replies: 3
    Last Post: 04-14-2008, 02:29 AM
  3. Using 'flags'
    By cyreon in forum C++ Programming
    Replies: 11
    Last Post: 01-28-2008, 03:21 PM
  4. Bit Flags
    By Padawan in forum C Programming
    Replies: 15
    Last Post: 03-30-2004, 09:38 PM
  5. Flags, the ambiguity of
    By Jeremy G in forum C++ Programming
    Replies: 1
    Last Post: 01-25-2003, 10:41 PM

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