Thread: fstream not .h

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246

    Question fstream not .h

    I use this when include fstream.h
    Code:
    ifstream obj(path,ios::nocreate)
    What should I write when using fstream? (with namespace)
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In the case of ifstream, it is not necessary. If the file does not exist, the ifstream won't open it and won't create it. Just remove the flag and check obj.is_open after the constructor to verify that the file was opened or not.

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    So, what if we want to force it to create a file?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I don't see why you'd want to force it to create a file if you are using an ifstream (which is for input files). If you are using an ofstream (for output files) or a plain fstream, then the file will be created automatically.

    If you are using an ofstream and you don't want to create the file (which is what nocreate was likely used for), then you could just use an ifstream to open the file for reading. If that succeeds, then close it and open the file for writing with your ofstream. If the ifstream open fails, then the file doesn't exist.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fstream. I/O
    By kevinawad in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2008, 09:19 AM
  2. ARGH! fstream errors
    By OttoDestruct in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2004, 10:37 PM
  3. Are fstream and ofstream incompatible?
    By johnnyd in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2003, 12:21 PM
  4. placement of (.h) and (.cpp) files..!?
    By matheo917 in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2003, 06:37 PM
  5. Problems with fstreams.
    By mosdef in forum C++ Programming
    Replies: 7
    Last Post: 06-19-2002, 03:36 PM