为什么我需要输入Ctrl-D两次标记档案结尾? [英] Why do I need to type Ctrl-D twice to mark end-of-file?

查看:812
本文介绍了为什么我需要输入Ctrl-D两次标记档案结尾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

char **query; 
query = (char**) malloc ( sizeof(char*) );

int f=0;
int i=0,j=0,c;


while((c=getchar())!=EOF)
{      
    if(!isalpha(c))
        continue;

    if(f==1)
        query=(char**) realloc(query,(i+1)*sizeof(char*));

    query[i]=(char*) malloc(sizeof(char));
    query[i][j]=c;
    j++;


    while( (c=getchar())!=EOF&&c!=' '&&c!='\t' )
    {      

        query[i]=(char*) realloc(query[i],(j+1)*sizeof(char));

        query[i][j]=c;
        ++j;
    }   

    query[i][j]='\0';
    printf("%s\n",query[i]);
    if(c==EOF){

        break;
    }   

   ++i;
   f=1;
   j=0;
}

我想上面的code段来读取一行由空格和制表符分隔,直到一个EOF字符串,但它需要2的EOF结束循环。此外,字符串可以只包含字母字符。

I want the above code snippet to read a line of strings separated by spaces and tabs until ONE EOF but it requires 2 EOFs to end the loop. Also, strings can consist of only alphabetic characters.

我奋力在2天左右。
请给一些反馈。

I am struggling on about 2 days. Please, give some feedback.

编辑:最有可能的原因是我按Ctrl + D键后,我写的最后一个字符串不回车键,但现在我按下回车键,然后按CTRL + D,它按预期工作。
但是,我能怎样改变来完成我打CTRL + D后,最后一个字符串以下一次?

Most probably the reason is I hit CTRL+D keys after I write last string not the enter key, but now I hit enter and then CTRL+D, it works as expected. But, how can I change it to finish after I hit CTRL+D once following the last string?

推荐答案

在类Unix系统(至少是默认设置),结束文件中的条件键入按Ctrl-D 触发在一行的开头的的按Ctrl-D的两次的,如果你在一行的开头不是。

On Unix-like systems (at least by default), an end-of-file condition is triggered by typing Ctrl-D at the beginning of a line or by typing Ctrl-D twice if you're not at the beginning of a line.

在后一种情况下,你读的最后一行将不会有一个的'\\ n'在它的结束;您可能需要允许这一点。

In the latter case, the last line you read will not have a '\n' at the end of it; you may need to allow for that.

这是指定的(而间接)POSIX /公开组基本规范问题7,在第11 ,具体11.1.9:

This is specified (rather indirectly) by POSIX / The Open Group Base Specifications Issue 7, in section 11, specifically 11.1.9:

EOF结果
  特殊字符输入,如果ICANON标志是识别
  组。接收时,所有等待读取的字节数将立即
  传递给过程,而不必等待一个与所述;换行符>和EOF为
  丢弃。因此,如果有任何字节等待(即,EOF
  在一行的开头)发生,零字节计数应
  来自read(),再presenting档案结尾的指示返回。如果
  设置了ICANON,处理时EOF字符将被丢弃。

EOF
Special character on input, which is recognized if the ICANON flag is set. When received, all the bytes waiting to be read are immediately passed to the process without waiting for a <newline>, and the EOF is discarded. Thus, if there are no bytes waiting (that is, the EOF occurred at the beginning of a line), a byte count of zero shall be returned from the read(), representing an end-of-file indication. If ICANON is set, the EOF character shall be discarded when processed.

在POSIX 阅读()函数表示结束文件(或错误)的条件下,以它的调用者通过返回零字节数,这表明不存在数据的多个字节来阅读。 (C的&LT;标准输入输出&GT; 是,在POSIX系统,建立在顶部阅读()和其他POSIX具体功能)。

The POSIX read() function indicates an end-of-file (or error) condition to its caller by returning a byte count of zero, indicating that there are no more bytes of data to read. (C's <stdio> is, on POSIX systems, built on top of read() and other POSIX-specific functions.)

EOF(不要用C EOF 宏混淆)默认情况下为<大骨节病>按Ctrl-D 映射。在一行(无论是在输入的开始或换行之后)的开头键入EOF字符会立即触发档案结尾条件。该行打字比在一行的开头其他EOF字符会导致previous数据进行下一个阅读(),要求足够的调用将立即返回字节;键入EOF字符的再次的做同样的事情,但在这种情况下,有要读无剩余字节和档案结尾的条件被触发。在一行的中间一个单一的EOF字符将被丢弃(如 ICANON 设置,它通常是)。

EOF (not to be confused with the C EOF macro) is mapped by default to Ctrl-D. Typing the EOF character at the beginning of a line (either at the very beginning of the input or immediately after a newline) triggers an immediate end-of-file condition. Typing the EOF character other than at the beginning of a line causes the previous data on that line to be returned immediately by the next read() call that asks for enough bytes; typing the EOF character again does the same thing, but in that case there are no remaining bytes to be read and an end-of-file condition is triggered. A single EOF character in the middle of a line is discarded (if ICANON is set, which it normally is).

这篇关于为什么我需要输入Ctrl-D两次标记档案结尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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