문제

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

도움이 되었습니까?

해결책 2

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

다른 팁

Most editors expect to work with normal files, so you'd typically create a temporary file, then pass the name of that file to the editor on its command line. When the editor returns, you copy the content from the temporary file into your log, then destroy the file.

what about open a file(passing file name to the editor), save and then read it from your program? I've always thought git works the same way.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top