0

Desktop/(ユーザーが指定したフォルダ)に「Control.h」という名前のファイルを作成し、そこにテキストを書き込みたいです。どうすればいいですか?(Macの場合).......これは私がこれまでに持っているものです:

#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);
4

1 に答える 1

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

しかし、なぜconst char*文字列を使用しcinているのですか? を使用するcin::getlinestd::string、バッファ オーバーフローを回避するために使用します。また、sprintf の使用も危険です。より良い解決策:

#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);
于 2014-02-16T22:47:44.227 に答える