Thread: Can't open file

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    61

    Can't open file

    Code:
    std::fstream outputFile;
    outputFile.open("Mods/CivTacticalCombat/BattleInfo/UnitInfo.dat", std::ios::in | std::ios::out | std::ios::binary);
    				
    FAssertMsg(outputFile, "Error opening file:Mods/CivTacticalCombat/BattleInfo/UnitInfo.dat");
    What is wrong with this code? Every time I run it outputFile has a false value, causing the assert to fail. I'm sure it's something really stupid that I'm overlooking, but I can't find it. I've been looking at C++ documentation for the last half hour trying to find what I'm doing wrong with no results.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You're trying to open a file in a relative path. If your executable file is in /myfile, then unless your file exists at /myfile/Mods/CivTacticalCombat/BattleInfo/UnitInfo.dat, your attempt to open it will fail. So that's the first thing to verify.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    First what operating system are you using? Second do you have the rights to write to BattleInfo directory? Next does the directory Mods/CivTaticalCombat/BatttleInfo exist?

    Jim

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    61
    @rags_to_riches
    Yes, the executable is in that file.

    @jimblumberg
    I'm using Windows XP SP3. And yes to the other two questions.

    What's puzzling me even more is that yesterday it worked, but then when I changed it from std:: ofstream to std::fstream (to allow me to read from the file as well) outputFile started returning false.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    One of the differences is that by default ofstream will create the file if it does not exist, you may want to try adding the ios::app flag if you want to add to the file or maybe ios::trunc to erase the file before use.

    Jim

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    You should also check the properties of your project and make sure that the current working directory is what it needs to be, that it points to your relative file path. A lot of the time, an IDE will set the current working directory to a default projects folder and that messes up everything.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    61
    Quote Originally Posted by jimblumberg View Post
    One of the differences is that by default ofstream will create the file if it does not exist
    fstream doesn't create a new file by default? This could be the problem...how do I set it to create a new file?

    EDIT: I just read that you can't open a nonexistent file for reading, so I guess I have to create the file. I'll try it out and see if it works.

    Quote Originally Posted by jimblumberg View Post
    ...or maybe ios::trunc to erase the file before use.
    EDIT2: Wait...doesn't it do this by default???
    Last edited by LyTning94; 12-28-2011 at 04:26 PM.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Not for an fstream object. The default open mode of a fstream is ios::in | ios::out which requires that the file exist. You need to add either app, or trunc to the open mode to create a file if it does not exist.
    Code:
    yourfile.open("YourFileName", std::ios::in | std::ios::out | std::ios::app); // or std::ios::trunc
    Jim

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    61
    It appears to be working fine now. I guess adding ios::trunc to open() is what I needed. Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I open a .dat file?
    By ahmedbatty in forum C Programming
    Replies: 3
    Last Post: 12-25-2011, 05:01 AM
  2. cant open file - says no file or directory
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 10:57 PM
  3. Replies: 3
    Last Post: 03-08-2010, 02:43 PM
  4. Open File/Save File crashes on cancel?
    By Blackroot in forum Windows Programming
    Replies: 0
    Last Post: 08-02-2008, 02:16 AM
  5. Replies: 12
    Last Post: 03-10-2005, 07:48 PM