我正在尝试在getline()
从打开的文本文件获取行的同时向窗口添加字符串。使用ncurses和c ++,我正在执行以下操作:
string line; //String to hold content of file and be printed
ifstream file; //Input file stream
file.open(fileName) //Opens file fileName (provided by function param)
if(file.is_open()) { //Enters if able to open file
while(getline(file, line)) { //Runs until all lines in file are extracted via ifstream
addstr(line); //LINE THAT ISN'T WORKING
refresh();
}
file.close(); //Done with the file
}
所以我的问题是...如果我想输出不是const数据类型的内容,我应该在ncurses中做什么?我没有看到in the documentation的任何输出函数接受任何but const input的内容。
应该注意的是,如果我只是将文件的内容输出到控制台,则该程序可以很好地工作,从而消除了文件读取/打开错误或流中内容的可能性。我在编译时得到的确切错误是:
错误:无法将参数“ 2”的“ std :: __ cxx11 :: string {aka std :: __ cxx11 :: basic_string}”转换为“ const char *”,转换为“ int waddnstr(WINDOW *,const char *,int)”
addstr(line);
让我知道您是否需要更多信息。
编辑:添加了相关文档的链接。
最佳答案
该问题与const
或与任何非const
的性质无关。
问题在于ncurses的addstr()
是C库函数,需要以N结尾的C样式字符串。
您试图将C ++ std::string
对象作为参数传递给addstr
()。鉴于addstr
()本身是C库函数,所以这不会很好地结束。
解决方案是使用std::string
的c_str
()方法获取C样式的字符串。
addstr(line.c_str());