fputs(),fgets(),ferror()问题和C ++等效 [英] fputs(), fgets(), ferror() questions and C++ equivalents

查看:220
本文介绍了fputs(),fgets(),ferror()问题和C ++等效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在阅读一本关于在C中编写shell的书,我想尝试用C ++编写它。我遇到了以下代码:

So I was reading a book on writing a shell in C and I want to try to write it in C++. I came across the following code:

for( ; ; ) 
{
    if (fputs(PROMPT, stdout) == EOF)
        continue;

    if (fgets(inbuf, MAX, stdin) == NULL)
        continue;

    //and so on....
}




  1. 我不明白 fputs()在这里的用法。

(a)如果stdout是终端,EOF有什么意义?

(a) if stdout is a terminal, does EOF have any meaning? What kind of errors can you get writing to a terminal except maybe the stream is already closed?

(b)如果stdout以前被重定向并且真的是一个管道或文件,那么你可以向终端写什么错误?几个不同的错误似乎可能。他们在哪里上市?见下面的(c)。

(b) if stdout was previously redirected and is really a pipe or file then several different errors seem possible. Where are they listed? See (c) below.

(c)上面的(b),ferror()似乎没有帮助。它的返回值是否映射到errno的值,因此与使用像perror()一样?

(c) following (b) above, ferror() doesn't seem that helpful. Does its return values map to those of errno and thus the same as using something like perror()? Where are the constants kept in order to do something like

  if (ferror() == SYSTEM_ERROR_13)

(d)在上面的代码中,如果fputs()返回一个错误为什么继续工作?

(d) in the code above, if fputs() did return an error why would a "continue" work? Wouldn't the error need to be cleared first with something like clearerr() or it would just repeatedly fail?

是C ++中的等效代码:

Is the equivalent code in C++:

for( ; ; ) 
{
    if (! cout << PROMPT)
    {
        cout.clear();
        continue;
    }

    if (! getline(cin, inbuf))
    {
        cin.clear();
        continue;
    }

    //and so on....
}



推荐答案

if (fputs(PROMPT, stdout) == EOF)
    continue;

a)如果stdout是终端,EOF有什么意义吗? - fputs 函数在错误时返回EOF。

a) if stdout is a terminal, does EOF have any meaning? - fputs function returns EOF on error.

b)不同的错误似乎可能。他们在哪里列出? - 写入stdout的原因失败真的很重要吗?

b) different errors seem possible. Where are they listed? - Does the reason why writing into stdout has failed really matter? Are you sure you want to go that deep?

c)ferror()似乎没有帮助...使用像perror()? / em>它们都基于全局变量 errno 工作。虽然 perror 会是更好的选择,因为它使输出指定格式为 stderr

c) ferror() doesn't seem that helpful ... using something like perror()? They both work based on the global variable errno. Although perror would be much better option for you, since it makes the output in specified format to stderr.

d)在上面的代码中,如果fputs 工作? - 这似乎不正确。

d) in the code above, if fputs() did return an error why would a "continue" work? - This seems to be incorrect.

根据这些事实,它应该看起来像这样:

According to these facts, it should look like this:

if (fputs(PROMPT, stdout) == EOF)
{
    perror("The following error occurred");
    exit(1);
}

if (fgets(inbuf, MAX, stdin) == NULL)
{
    perror("The following error occurred");
    continue;
}

2。是C ++中的等效代码吗? - 不。有一点区别: fgets 读取该行,\\\
包含在字符串中,而 getline 读取行但不存储分隔符('\\\
')。

2. Is the equivalent code in C++ ? - No. There is one difference: fgets reads the line and '\n' is included in the string, while getline reads the line but delimiter ('\n') is not stored.

这篇关于fputs(),fgets(),ferror()问题和C ++等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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