Perplexity Pro 1

LINEMOを契約してPerplexity Proを一年間のあいだ無料で使います。

Ask 1

仕事でC++で環境変数を使いたい場面が発生。


tell me example of usage getenv function by C++

(no file)

#include <iostream>
#include <cstdlib>

int main() {
    const char* path = getenv("PATH");

    if (path != nullptr) {
        std::cout << "PATH: " << path << std::endl;
    } else {
        std::cout << "PATH environment variable not found." << std::endl;
    }

    return 0;
}

Ask 2

getenv()はC言語の関数でconst char *型を返す。std::stringが欲しい。


make file name using text by getenv function

(no file)

#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>

int main() {
    const char* home_dir = getenv("HOME");

    if (home_dir != nullptr) {
        std::string filename = std::string(home_dir) + "/example.txt";

        std::ofstream file(filename);
        if (file.is_open()) {
            file << "This is a test file." << std::endl;
            file.close();
            std::cout << "File created: " << filename << std::endl;
        } else {
            std::cout << "Unable to create file: " << filename << std::endl;
        }
    } else {
        std::cout << "HOME environment variable not found." << std::endl;
    }

    return 0;
}

解決。

まとめ

部下のA君より優秀。

広告

IT開発関連書とビジネス書が豊富な翔泳社の通販『SEshop』
さくらのレンタルサーバ
ムームードメイン
Oisix(おいしっくす)
らでぃっしゅぼーや
珈琲きゃろっと
エプソムソルト




«       »