this question numaralı girdiyi buldum; bu, giriş eşzamansız olarak nasıl okunacağını sorar, ancak yalnızca Windows üzerinde çalışmayan POSIX akış tanımlayıcılarıyla çalışır. Bu yüzden, bir POSIX akış tanımlayıcısı kullanmak yerine boost::asio::windows::stream_handle
kullanabileceğimi gösteren this tutorial'u buldum.Windows'taki yükseltmeyi kullanarak komut satırındaki giriş eşzamansız olarak nasıl okunur?
Her iki örneği de takiben aşağıdaki kodla geldim. Çalıştırdığımda, programın hemen sona ermesiyle, komut istemine herhangi bir şey yazamam. Programımdaki diğer mantığın çalıştırılmasına izin verirken (örneğin, Windows konsolundan eşzamansız I/O gerçekleştirin), kullanıcının herhangi bir girişini, muhtemelen bir std::string
içine çekmesini istiyorum.
Temelde, stdin
'dan okumaya çalışırken programımı engellemekten kaçınmaya çalışıyorum. Ben de aynı şeyi yapmaya çalışırken başka bir kullanıcı karşılaşılan sorunları ayrıntılar this post bulundu gibi, Windows mümkün olup olmadığını bilmiyorum.
#define _WIN32_WINNT 0x0501
#define INPUT_BUFFER_LENGTH 512
#include <cstdio>
#include <iostream>
#define BOOST_THREAD_USE_LIB // For MinGW 4.5 - (https://svn.boost.org/trac/boost/ticket/4878)
#include <boost/bind.hpp>
#include <boost/asio.hpp>
class Example {
public:
Example(boost::asio::io_service& io_service)
: input_buffer(INPUT_BUFFER_LENGTH), input_handle(io_service)
{
// Read a line of input.
boost::asio::async_read_until(input_handle, input_buffer, "\r\n",
boost::bind(&Example::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void handle_read(const boost::system::error_code& error, std::size_t length);
void handle_write(const boost::system::error_code& error);
private:
boost::asio::streambuf input_buffer;
boost::asio::windows::stream_handle input_handle;
};
void Example::handle_read(const boost::system::error_code& error, std::size_t length)
{
if (!error)
{
// Remove newline from input.
input_buffer.consume(1);
input_buffer.commit(length - 1);
std::istream is(&input_buffer);
std::string s;
is >> s;
std::cout << s << std::endl;
boost::asio::async_read_until(input_handle, input_buffer, "\r\n",
boost::bind(&Example::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
else if(error == boost::asio::error::not_found)
{
std::cout << "Did not receive ending character!" << std::endl;
}
}
void Example::handle_write(const boost::system::error_code& error)
{
if (!error)
{
// Read a line of input.
boost::asio::async_read_until(input_handle, input_buffer, "\r\n",
boost::bind(&Example::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
}
int main(int argc, char ** argv)
{
try {
boost::asio::io_service io_service;
Example obj(io_service);
io_service.run();
} catch(std::exception & e)
{
std::cout << e.what() << std::endl;
}
std::cout << "Program has ended" << std::endl;
getchar();
return 0;
}
Windows kullanıcısı değilim, ancak yeni bir satır göstergesi olarak \ r \ n kullanmıyor mu? –
Evet, ancak çalışmayı engelleyecektir, çünkü \ n hala yeni satır sırasına mı giriyor? Aynı dizede "\ r \ n" için sınır dizesini değiştirdim. – nickb
nerede io_service :: run()? –