0

I want to create file named "Control.h" in Desktop/(specified folder by user), and write text to it. How do I do this? (for mac)........ This is what I have so far:

#include <iostream>
#include <fstream>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

int main ()
{
    char game_name [100];
        cout << "Game Name: ";
        cin >> game_name;

        const char* homeDir = getenv ("HOME");
        char final [256];
        sprintf (final, "%s/Desktop/%s",homeDir, game_name);
        mkdir(final,0775);
user3316925
  • 23
  • 2
  • 4
  • in that one i was asking how to make it go to Desktop. I'm now asking how to create a folder in Desktop/givenName and create files insdide it with name – user3316925 Feb 16 '14 at 22:48

1 Answers1

0
std::ofstream out(std::string(final)+"/Control.h");
// ...
out << mytext; // write to stream
// ...
out.close();

Why are you using const char* strings with, cin, though? Either use cin::getline or use std::string to avoid buffer overflows. In addition, using sprintf is also dangerous. A better solution:

#include <string>
// ...
{
    std::string game_name [100];
    cout << "Game Name: ";
    cin >> game_name;

    std::string homeDir = getenv ("HOME");
    std::string final=homeDir+"/Desktop/"+game_name;
    mkdir(final.c_str(),0775);
kirbyfan64sos
  • 10,377
  • 6
  • 54
  • 75