有一条正常的线
'\n'
最后。不是这样的
Ctrl键
+
丁
在Unix的land shell中。例如,
#include <stdio.h>
#include <unistd.h> // read
void shell()
{
char line[256];
for( bool finished = false; not finished; )
{
printf( ">>> " );
//fgets( line, sizeof( line ), stdin );
fflush( stdout );
const int n_bytes = read( 0, line, sizeof( line ) - 1 );
line[n_bytes] = '\0';
char const* p = line;
finished = true;
while( char const input = *p++ )
{
if( input == '\n' )
{
finished = false;
break;
}
printf( "[%c]", input );
}
printf( "\n" );
}
}
auto main()
-> int
{
printf( "before shell\n" );
shell();
printf( "shell has exited\n" );
}
请您解决以下问题:
-
处理EOF(空行按下)。
-
重写C++ C++流,而不是C
FILE*
输入/输出。
-
用一个
Ctrl键
+
丁
-按下输入行控制台输出中缺少换行符。
注1:
read
通常也可用于Windows编译器。然而,
注2:
Ctrl键
+
丁
推动当前行是一个Unix的land约定。如果你想让你的程序表现出这种行为,不管它是如何运行或在什么系统上运行的,你必须使用一些可移植的低级字符输入库,比如ncurses。