当前位置: 技术问答>linux和unix
终端non-canonical方式后,读取string的问题。
来源: 互联网 发布时间:2016-06-27
本文导语: 以下程序,在从屏幕读取char时(getchar()),terminal会按照non-canonical的方式显示处理输入数据,而在读取string时(gets()),则不会,为什么? #include #include #include #include int main() { char str[10] = {0}; char c; ...
以下程序,在从屏幕读取char时(getchar()),terminal会按照non-canonical的方式显示处理输入数据,而在读取string时(gets()),则不会,为什么?
#include
#include
#include
#include
int main()
{
char str[10] = {0};
char c;
int rlt;
FILE* in;
struct termios attr;
if(!isatty(fileno(stdout)))
{
assert(0);
}
in = fopen("/dev/tty", "r");
tcgetattr(fileno(in), &attr);
attr.c_lflag &= ~ICANON;
attr.c_lflag &= ~ISIG;
attr.c_cc[VMIN] = 2;
attr.c_cc[VTIME] = 0;
rlt = tcsetattr(fileno(in), TCSANOW, &attr);
puts("input char");
c = getchar();
puts("input string");
gets(str);
}
#include
#include
#include
#include
int main()
{
char str[10] = {0};
char c;
int rlt;
FILE* in;
struct termios attr;
if(!isatty(fileno(stdout)))
{
assert(0);
}
in = fopen("/dev/tty", "r");
tcgetattr(fileno(in), &attr);
attr.c_lflag &= ~ICANON;
attr.c_lflag &= ~ISIG;
attr.c_cc[VMIN] = 2;
attr.c_cc[VTIME] = 0;
rlt = tcsetattr(fileno(in), TCSANOW, &attr);
puts("input char");
c = getchar();
puts("input string");
gets(str);
}
|
gets需要等到回车符号呢!!!
The fgets() function reads at most one less than the number of characters
specified by size from the given stream and stores them in the string
str. Reading stops when a newline character is found, at end-of-file or
error. The newline, if any, is retained. If any characters are read and
there is no error, a `' character is appended to end the string.
The gets() function is equivalent to fgets() with an infinite size and a
stream of stdin, except that the newline character (if any) is not stored
in the string. It is the caller's responsibility to ensure that the
input line, if any, is sufficiently short to fit in the string.
The fgets() function reads at most one less than the number of characters
specified by size from the given stream and stores them in the string
str. Reading stops when a newline character is found, at end-of-file or
error. The newline, if any, is retained. If any characters are read and
there is no error, a `' character is appended to end the string.
The gets() function is equivalent to fgets() with an infinite size and a
stream of stdin, except that the newline character (if any) is not stored
in the string. It is the caller's responsibility to ensure that the
input line, if any, is sufficiently short to fit in the string.
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。