Mazzone
Mazzone

Reputation: 308

Ncurses output functions only accept const data

I'm trying to add strings to the window while getline() is getting lines from the opened text file. Using ncurses and c++, I am doing the following:

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                                                                                                                                                                                                 
} 

So my question is... what am I supposed to do in ncurses if I want to output something that isn't a const data type? None of the output functions I see in the documentation accept anything but const input.
It should be noted that this program works perfectly fine if I am just outputting the content of the file to the console, so that removes the possibility of it being a file reading/opening error or something with the stream. The exact error I get on compilation is:

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

Let me know if you need any more info.

EDIT: Added links to relevant documentation.

Upvotes: 0

Views: 618

Answers (1)

Sam Varshavchik
Sam Varshavchik

Reputation: 118445

The problem doesn't have anything to do with constness, or non-constness of anything, directly.

The problem is that ncurses' addstr() is a C library function, that expects a null-terminated C style string.

You are attempting to pass, as an argument, a C++ std::string object, to addstr(). Given that addstr() itself is a C library function, this is not going to end very well.

The solution is to use std::string's c_str() method to get a C-style string.

addstr(line.c_str());

Upvotes: 4

Related Questions