Sarah
Sarah

Reputation: 1

Trouble with mkdir and ofstream

This is my first time working with directories.

I have this code snippet:

void initialize()
{
  mkdir("/cydrive/c/enc/user", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  mkdir("/cygdrive/c/enc/misc", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  ofstream user ("/cygdrive/c/enc/misc/usercount.txt");
  if (user.is_open())
    user << "0 0" << endl;
  user.close();
  ofstream prompt ("/cygdrive/c/enc/misc/prompt.txt");
  if (prompt.is_open())
    prompt << "CLI>";
  prompt.close();  
  ofstream randomuser ("/cydrive/c/enc/user/rando.txt");
  if (randomuser.is_open())
    randomuser << "garbageinfo";
  randomuser.close();
}

The user and prompt ofstreams are behaving exactly as I intend, but whenever I try to open an enc/user directory it simply will not open. This inconsistency is driving me crazy, any idea what is happening?

Upvotes: 0

Views: 551

Answers (1)

hmjd
hmjd

Reputation: 121971

I suspect the mkdir() is failing due to a typo in the directory name. I think:

mkdir("/cydrive/c/enc/user", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);

should be:

mkdir("/cygdrive/c/enc/user", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
          ^

If not, check the return value of mkdir() to ensure success:

if (0 != mkdir("/cydrive/c/enc/user", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) &&
    EEXIST != errno)
{
    std::cerr << "Failed to create directory: "<< strerror(errno) << "\n";
}

Upvotes: 1

Related Questions