Ncurses 输出函数只接受常量数据
Ncurses output functions only accept const data
我正在尝试向 window 添加字符串,而 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 中做什么? None 的输出函数我看到 in the documentation accept anything but const input.
应该注意的是,如果我只是将文件的内容输出到控制台,那么这个程序工作得很好,这样就消除了它是文件 reading/opening 错误或流中的某些东西的可能性。我在编译时得到的确切错误是:
error: cannot convert ‘std::__cxx11::string {aka std::__cxx11::basic_string}’ to ‘const char*’ for argument ‘2’ to ‘int waddnstr(WINDOW*, const char*, int)’
addstr(line);
如果您需要更多信息,请告诉我。
编辑:添加了相关文档的链接。
这个问题与const
性或非const
性没有任何直接关系。
问题是 ncurses 的 addstr()
是一个 C 库函数,需要一个以 null 结尾的 C 风格字符串。
您正试图将 C++ std::string
对象作为参数传递给 addstr
()。鉴于 addstr
() 本身是一个 C 库函数,这不会很好地结束。
解决方案是使用std::string
的c_str
()方法来获取C风格的字符串。
addstr(line.c_str());
我正在尝试向 window 添加字符串,而 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 中做什么? None 的输出函数我看到 in the documentation accept anything but const input.
应该注意的是,如果我只是将文件的内容输出到控制台,那么这个程序工作得很好,这样就消除了它是文件 reading/opening 错误或流中的某些东西的可能性。我在编译时得到的确切错误是:
error: cannot convert ‘std::__cxx11::string {aka std::__cxx11::basic_string}’ to ‘const char*’ for argument ‘2’ to ‘int waddnstr(WINDOW*, const char*, int)’ addstr(line);
如果您需要更多信息,请告诉我。
编辑:添加了相关文档的链接。
这个问题与const
性或非const
性没有任何直接关系。
问题是 ncurses 的 addstr()
是一个 C 库函数,需要一个以 null 结尾的 C 风格字符串。
您正试图将 C++ std::string
对象作为参数传递给 addstr
()。鉴于 addstr
() 本身是一个 C 库函数,这不会很好地结束。
解决方案是使用std::string
的c_str
()方法来获取C风格的字符串。
addstr(line.c_str());