ç等待标准输入读取? [英] c wait for stdin read?

查看:129
本文介绍了ç等待标准输入读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我想实现这样的:

in my application, i'm trying to achieve something like this:

我有:


  • 数据0,数据1,数据2,...数据n。

  • 某些参数传递

流量:


  1. 开始以一些参数的程序和0数据写入标准输入

  2. 根据传递数据的数据0和参数
  3. 程序所做的计算

  4. 等待的新标准输入和(清除旧标准输入,缓冲和变量?)

  5. 重复1〜2,当我把数据1,数据2 ...等等

  6. 当达到数据n,终止(或者,如果我输入中止code到标准输入告诉程序终止)。

也许是这样的(伪code):

maybe something like this?(pseudo code):

int main(int argc, char *argv[])
{
get parameters();
int fslen = data size
char *c = (char *)malloc(fslen);
fgets(c, fslen, stdin);

while((c != null) || (c != Terminate code?))
{       
    do calculations with int c;
    clear c;
}
return 0;
}

或者是他们的一个更好的方法?

or is their a better approach?

或只是不好的做法,这样做事?
如果是这样,请解释

or is it just bad practice to do things this way? if so, please explain

推荐答案

有真的没有什么更好的办法,至少就我所知,读取和解析线路输入比读取和解析线路输入。

There's really not any better way, at least as far as I'm aware, to read and parse line input than to read and parse line input.

在默认情况下,标准输入应该是堵,让你的等待的标准应采取的自动的照顾。

By default, stdin should be blocking, so your 'waiting' criterion should be taken care of automagically.

您将,但是,需要两个循环,如果你打算要读行,然后解析C $ CS为$行:

You will, however, need two loops if you're going to be reading lines and then parsing the lines for codes:

int main(int argc, char *argv[])
{
    /* initial parameter/buffer setup goes here */

    while (fgets(buffer, bufferlen, stdin)) {
        for (c = buffer; *c != '\0' && *c != terminatingcode; c++) {
            /* calculations go here! ... they sure do! </homer> */
        }
        if (*c == terminatingcode || ferror(stdin))
           break;
    }
}

请注意,与fgets()可以不及格这里完全无害的原因,你需要熟悉的feof()和ferror(),以确保您正确使用界面;我不能确定我在上面的code使用是否符合你想要什么/需要code ++做的是一致的。

Be aware that fgets() can "fail" here for perfectly innocent reasons, and you need to become familiar with feof() and ferror() to make sure you're using the interface correctly; I'm unsure whether my use in the code above is consistent with what you want/need the code to do.

这篇关于ç等待标准输入读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆