用户手动输入输出路径
Users Manually Enter Input and Output Paths C++
我对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;
}
}
相关文章:
- 字符的输入/输出操作
- C++ 中文件输入输出中的长长 v/s int
- 要求用户输入C++输出文件名
- 在C++中重载输入/输出运算符
- 从用户获取输入/输出
- 如何选择waveIn/waveOut函数C++使用的输入/输出设备
- C++击球平均值程序使用输入/输出文件打印名称,平均值,最高/最低平均值时遇到麻烦
- 从 2 位输入输出 4 位数字年份
- 标准输入输出操作如何C++完成
- Windows 中应用程序的每秒输入/输出操作数
- 如何初始化可以设置为默认数组并将输入/输出作为数组的类函数?
- 输入/输出和类
- 使用 Octave GNU 访问输入/输出文件
- (C++)分解这个简单的输入/输出代码:
- TensorFlow模型:如何从原始爱好文件中识别输入/输出节点名称
- 干净取消在输入/输出调用中被阻止的 std::线程
- 将线程之间的标准输入/输出重定向
- 输入/输出异或字符串的 C++ 问题
- C++输入/输出错误
- 用户手动输入输出路径