用户手动输入输出路径

Users Manually Enter Input and Output Paths C++

本文关键字:路径 输入输出 用户      更新时间:2023-10-16

我对c++一无所知,但我被分配编辑这段代码:

// Setup path information for output file from environmental variables
char * path = new char[100];
path = getenv("MODEL_MERGE");
char * templatePath = new char[100];
char * outputPath = new char[100];
strcpy(templatePath, path);
strcat(templatePath, "infile location");
strcpy(outputPath, path);
strcat(outputPath,"outfile location");
cout << "temp: " << templatePath << endl;
cout << "out:  " << outputPath << endl;
//input output file streams for reading/writing to files
ifstream readFile(templatePath);
ofstream outFile(outputPath); 

我的目标是替换当前指向特定文件的"inffile location"answers"outfile location"。我希望用户能够在从命令提示符运行时输入文件名。对不起,如果这是像<<cin一样简单的东西,但我无法让它工作,而且我对这种语言没有任何经验。

得到它!以上所有内容替换为:

//User inputs paths
    string input;
    string output;
    cout<<"Input path?"<<endl;
    cin>> input;
    cout<<"output path?"<<endl;
    cin>> output;   

//input output file streams for reading/writing to files
ifstream readFile(input.c_str());
ofstream outFile(output.c_str());`

谢谢大家的帮助!

提供给OP的代码中有足够多的错误,除了为OP指出有用的方向外,还值得快速浏览一下。

首先,在调用getenv时不测试NULL。如果MODEL_MERGE不存在,则返回NULL,然后在字符串副本中使用。繁荣!

第二步,new所有这些数组。动态分配只作为最后的手段。new必须与至少一个delete进行比较,这取决于代码的流,以便在不再需要时返回分配的内存以供重用。由于似乎不需要动态分配,并且数组的大小是已知的,因此应该将它们定义为char templatePath[100];。需要处理的内存管理更少,有效地避免了泄漏的可能性。

第三点使第二点过时。尽可能使用字符串,而不是使用字符数组。它们不仅为您处理所有的内存管理,包括根据需要调整大小,而不是超出范围,它们还可以轻松地执行复制和追加等日常任务。我将在下面演示这一点。

正确使用cin和cout在许多网站上都有详细的说明,所以我不在这里赘述。

还请注意,我已经通过显式声明使用的名称空间来删除对using namespace std;的需求。阅读为什么using namespace std;通常是一个坏主意。

#include <fstream>
#include <iostream>
int main()
{
    char * Model_MergePath = getenv("MODEL_MERGE");
    if (Model_MergePath != NULL)
    { //MODEL_MERGE is defined
        std::string path(Model_MergePath); //replace icky and fault-prone char array
        std::string templatePath = path; // copy strings with =
        std::string outputPath; // not assigning path here so I can demonstrate 
                                //something else later
        std::string inFileLoc; // new throw away variables for user input.
        std::string outFileLoc; // could use the same var for both. I didn't for clarity
        std::cin >> inFileLoc; // get input
        templatePath += inFileLoc; // append to strings with += 
        std::cin >> outFileLoc;
        outputPath = path + outFileLoc; // concatenate strings with +
        // validate paths for correctness and possible intrusion attempts here
        // this, I'm afraid, is up to the OP as the security requirements are unknown
        std::cout << "temp: " << templatePath << std::endl;
        std::cout << "out:  " << outputPath << std::endl;
        //input output file streams for reading/writing to files
        std::ifstream readFile(templatePath); 
        // older C++ compilers may require a c-style string as the file path
        std::ofstream outFile(outputPath.c_str());
        // do stuff with readFile and outFile
        // remove the deletes that should have corresponded to the replaced `new`s
        return 0;
    }
    else
    { //MODEL_MERGE is NOT defined
        std::cerr << "Cannot find environment variable MODEL_MERGE. Exiting." << std::endl;
        return -1;
    }
}