gpt4 book ai didi

c++ - 用户手动输入输入和输出路径 C++

转载 作者:行者123 更新时间:2023-11-28 02:22:03 25 4
gpt4 key购买 nike

我对 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);

我的目标是替换当前指向特定文件的“infile location”和“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 必须与至少一个 delete 配对,这取决于代码的流程,以在不再需要时返回分配的内存以供重用。由于似乎不需要动态分配并且数组的大小已知,因此它们应该被定义为 char templatePath[100];。需要处理的内存管理更少,并且有效地消除了泄漏的可能性。

第三点使第二点过时。不要使用 char 数组,而是尽可能使用字符串。它们不仅为您处理所有内存管理,包括根据需要调整大小而不是越界,它们还执行例行任务,例如复制和附加,而不是大惊小怪。我将在下面演示这一点。

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

另请注意,我已通过明确说明使用的命名空间来消除对 using namespace std; 的需要。 Read why using namespace std; is often a bad idea.

#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;
}
}

关于c++ - 用户手动输入输入和输出路径 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32060481/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com