I am creating a simple logging program. When a user enters log some_file
into the console, the program currently simply receives some basic input from cin
, and records it into some_file
.
However, instead of implementing my own editor with cin
, I'd like to open the Nano editor and let the user edit his message there.
Then, when the message is complete, I'd like my C++ logger to receive it as a string and carry on.
This is exactly what git does on commits.
How can I achieve this?
(Preferably without using tools such as expect, just raw C++ code.)
If you want to use the Nano editor then you need to run the system() function to invoke Nano with a temporary file. Then remove the file later..
std::string filename = "/tmp/.out." + std::to_string(getpid());
std::string cmd = "/bin/nano " + filename
system(cmd.c_str());
// read from filename
unlink(filename.c_str());
Update
If using tmpnam() as suggested by DevSolar
char filename[L_tmpnam];
tmpnam(filename);
std::string cmd = "/bin/nano " + filename
system(cmd.c_str());
unlink(filename);