7 Replies - 4869 Views - Last Post: 26 November 2011 - 03:33 AM Rate Topic: -----

#1 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

ofstream won't create directories & folders in Program Files.

Posted 23 November 2011 - 01:05 PM

I have this code that creates files essential to another program working (kinda like one of the things installers do). I could actually use an installer for this but programming's fun so I decided to do it in code rather than a series of boring clicking. The problem is that the files will not actually be created. The while loops will execute and everything will be fine except that the files are not created. Last I checked, ofstream objects created new directories and files if the file-path was not already existent. The code executes.

<edit>Come to think of it, is Program Files special?</edit>

Here's the code:
#include <iostream>
#include <fstream>

using namespace std;

bool nextChar(char &param);

int main(void)
{
    ifstream inFile;
    bool hasRunBefore;
    inFile.open("C:\\Program Files\\ID tracker\\has run.txt");
    if(inFile)
    {
        cout << "Running this program can cause data-loss. If you have stored \
                 any data already, please save it before continuing.\nPress \
                 enter to continue . . ." << endl;
        cin.sync();
        cin.get();
    }
    else
    {
        ofstream outFile;
        outFile.open("C:\\Program Files\\ID tracker\\has run.txt");
        outFile << "Do not delete this file.\n\
                    Feel free to modify it though.\n\
                    Just make sure this file exists.";
        outFile.close();
    }
    char char1, char2;
    char1 = char2 = 96;
    ofstream outFile;
    string pathName;
    while(nextChar(char1))
    {
        while(nextChar(char2))
        {
            pathName = (string) "C:\\Program Files\\ID tracker\\Database\\" + char1 + "\\" + char2 + ".txt";
            outFile.open(pathName.c_str());
            // Words with a sharp cannot be a result. Therefore, comment lines should include sharps.
            outFile << "#Words_beginning_with:_" << char1 << char2 << "..." << endl;
            cout << "Created " << pathName << endl;
            outFile.close();
        }
    }
    cout << "The database has been constructed.\n"
         << "Press enter to exit . . ." << endl;
    cin.sync();
    cin.get();
    return 0;
}

bool nextChar(char &param)
{
    // If the char is 'Z' then break outta main's loop.
    // Else increment it and return true to remain in the loop.
    if(param == 122)
        param = 48;
    else if(param == 57)
    {
        param = 96;
        return false;
    }
    ++param;
    return true;
}



Thanks for reading this topic.

This post has been edited by hulla: 23 November 2011 - 01:07 PM


Is This A Good Question/Topic? 0
  • +

Replies To: ofstream won't create directories & folders in Program Files.

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: ofstream won't create directories & folders in Program Files.

Posted 23 November 2011 - 01:18 PM

Yes, Program Files *is* special, at least in the latest versions of Windows. Requires admin privileges to write there. Put your file in an approved place, such as the user's data directory, which you can get from the registry and/or Win API.
Was This Post Helpful? 1
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,434
  • Joined: 25-December 09

Re: ofstream won't create directories & folders in Program Files.

Posted 23 November 2011 - 01:36 PM

Also you will need to create the directory if it does not exist. The iostreams only work with existing directories. See this link for information for creating a directory in Windows CreatDirectory. Or checkout Boost.Filesystem

Jim

This post has been edited by jimblumberg: 23 November 2011 - 01:37 PM

Was This Post Helpful? 1
  • +
  • -

#4 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: ofstream won't create directories & folders in Program Files.

Posted 24 November 2011 - 12:40 AM

Isn't the CreatDirectory only for the WinAPI? It has WinAPI code.

I'll download Boost now. :)
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,434
  • Joined: 25-December 09

Re: ofstream won't create directories & folders in Program Files.

Posted 24 November 2011 - 07:09 AM

Yes CreatDirectory is part of the Windows API but since your program is running under Windows, "C:\\Program Files\\ID tracker\\has run.txt" is a Windows directory, it should work just fine.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: ofstream won't create directories & folders in Program Files.

Posted 25 November 2011 - 03:32 AM

Oh wait Linux and Mac don't have Program Files? :( I'll have to change the whole program then to a solution that can work for all so nevermind the Program Files idea.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,434
  • Joined: 25-December 09

Re: ofstream won't create directories & folders in Program Files.

Posted 25 November 2011 - 07:10 AM

If you are trying to create a program to run on multiple operating systems, don't hard code any paths. Get the path information from the user through command line parameters, or some other method, like a configuration file in the working directory. "C:\\Program Files\\ID tracker\\has run.txt" is only a valid path on Windows, not because of the "Program Files" but because of the "\\" both Linux and the current Mac OS use "/" for path separators. Also neither the Mac or Linux use drive letters. The above path is a valid Linux or Mac file name, it would create a file with the name of "C:\Program Files\ID tracker\has run.txt" in the current working directory.

Jim

This post has been edited by jimblumberg: 25 November 2011 - 07:13 AM

Was This Post Helpful? 0
  • +
  • -

#8 RetardedGenius  Icon User is offline

  • >>──(Knee)──►
  • member icon

Reputation: 126
  • View blog
  • Posts: 555
  • Joined: 30-October 10

Re: ofstream won't create directories & folders in Program Files.

Posted 26 November 2011 - 03:33 AM

I'd recommend using Boost over the Windows API for portability. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1