从 stdin 读取特定数量的字符

发布于 2024-11-08 15:43:26 字数 934 浏览 0 评论 0原文

您好,

我正在编写一个充当服务器的程序,并且能够通过终端与用户交互。如果我从标准输入读取最多 140 个字符。 如果用户输入超过 140 个字符,我只想获取前 140 个字符并忽略其余字符。

我尝试类似以下代码:

struct timeval timeout;
fd_set readings;
char buf[140];


while (1) {
    timeout.tv_sec = 15;
    timeout.tv_usec = 0;
    FD_ZERO(&readings);
    FD_SET(STDIN_FILENO,&readings);

    int rv = select(STDIN_FILENO+1,&readings,NULL,NULL,&timeout);
    if (rv<0) {
        cout << "ERROR select\n";
        exit(1);
    }
    if (rv==0) {
        cout << "Still Waiting...\n";
    }
    else {
        cout << "A key was pressed\n";
        if (FD_ISSET(STDIN_FILENO,&readings)) {
            int num = read(STDIN_FILENO,&buf,140);
            buf[num]='\0';
            cout << buf << endl;
        }
    }
}

问题是 - 当我输入超过 140 个字符时,将打印前 140 个字符,但随后 read() 在下一次迭代中读取其余数据并打印它。 我如何清除标准输入,以便在下一次迭代中忽略其余数据?

谢谢你!!

HI

I'm writing a program that acts as a server and has the ability to interact with the user via the terminal. if i read from the stdin i want up to 140 chars.
if the user enters more than 140 i would like to take only the first 140 chars and ignore the rest.

i try something like the following code:

struct timeval timeout;
fd_set readings;
char buf[140];


while (1) {
    timeout.tv_sec = 15;
    timeout.tv_usec = 0;
    FD_ZERO(&readings);
    FD_SET(STDIN_FILENO,&readings);

    int rv = select(STDIN_FILENO+1,&readings,NULL,NULL,&timeout);
    if (rv<0) {
        cout << "ERROR select\n";
        exit(1);
    }
    if (rv==0) {
        cout << "Still Waiting...\n";
    }
    else {
        cout << "A key was pressed\n";
        if (FD_ISSET(STDIN_FILENO,&readings)) {
            int num = read(STDIN_FILENO,&buf,140);
            buf[num]='\0';
            cout << buf << endl;
        }
    }
}

the problem is - when i enter more than 140 chars, the first 140 chars are printed, but then read() reads the rest of the data in the next iteration and prints it .
how do i clear the stdin so that the rest of the data will be ignored in the next iteration?

thank you!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

睫毛溺水了 2024-11-15 15:43:26

您可以修改您的解决方案以使用 readsome()

char c[140];
cin.readsome(&c[0], 140);

或者保留现有的并使用忽略

You could probably modify your solution to use readsome().

char c[140];
cin.readsome(&c[0], 140);

Or keep what you have and use ignore

小耗子 2024-11-15 15:43:26

几乎唯一的选择是读取字符并丢弃它们,直到到达行尾。

Pretty much the only choice is to read the characters and discard them until end-of-line is reached.

不甘平庸 2024-11-15 15:43:26

读取指针中的所有输入文本,然后提取前 140 个字符,然后再次删除指针,为下一个请求做好准备。

Read all the input text in a pointer, then extract the first 140 characters, and then delete the pointer again to be ready to the next request.

鸠魁 2024-11-15 15:43:26

你为什么不做这样的事情:

char buf[256];//what ever
...
int num = read(STDIN_FILENO,&buf,256);
if (num > 140) num = 140;
buf[num]='\0';
cout << buf << endl;

Why don't you do something like that:

char buf[256];//what ever
...
int num = read(STDIN_FILENO,&buf,256);
if (num > 140) num = 140;
buf[num]='\0';
cout << buf << endl;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文