目前,我正在编写一个行编辑器作为学习项目,涉及I/O、文件写入等方面。它是用C++编写的,我目前正试图将内容写入用户选择的文件中。我已经实现了CLI参数,但我目前不知道如何在程序中指定要写入的文件。
char *filename;
if (argc >= 2){
filename = argv[1];
} else{
cout << "file>";
cin >> filename;
cin.ignore();
}
当我使用命令行参数时,这个程序运行得非常好; 然而,每当我不使用命令行参数时,一旦我启动该程序,它就会发生分段错误。 我使用实际文件名的地方是在保存命令中:
void save(char filename[], int textlen, string file[]){
ofstream out(filename);
out << filestring(textlen, file);
out.close();
}
这也完美地运作。你能帮我吗?完整的源代码供审核,可在https://github.com/GBGamer/SLED上找到。
std::string
并阅读此文(自我推广)。 - BoBTFishc_str()
,它返回类型为const char *。 - suspectus