C语言 如何通过 Windows 终端发送 EOF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16136400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:07:11  来源:igfitidea点击:

How to send EOF via Windows terminal

cwindows

提问by 804b18f832fb419fb142

I'm trying to comprehend Example 1.9 from the K&R book, but I don't get how to send EOF. Some sources mentioned Ctr+Z, but that simply terminates the program. I somehow managed to send EOF with a combination of Enter and Ctrl+Z and maybe Ctrl+V, but I can't reproduce it.

我试图理解 K&R 书中的示例 1.9,但我不知道如何发送 EOF。一些消息来源提到了 Ctr+Z,但这只是终止了程序。我以某种方式设法通过 Enter 和 Ctrl+Z 以及 Ctrl+V 的组合发送 EOF,但我无法重现它。

#include <stdio.h>
#define MAXLINE 1000

main()
{
    int len;
    int max;
    char line[MAXLINE];
    char save[MAXLINE];

    max = 0;
    while((len = getline_my(line, MAXLINE)) > 0)
    if(len > max) {
        max = len;
        copy(line, save);
    }
    if(max > 0)
        printf("%s", save);
}

getline_my(s, lim)
char s[];
int lim;
{
    int c, i;

    for(i=0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; i++)// As long as the condition is fulfilled
        s[i] = c;
    if (c == '\n') {
        s[i] = c;
        i++;
    }
    s[i] = '
int main(){
    char ch[100];    
    scanf("%[^EOF]",ch);    
    printf("\nthe string is:\n%s\n",ch);    
    fflush(stdin);    
    return 0;    
    }
'; return(i); } copy(s1, s2) char s1[]; char s2[]; { int i; i = 0; while((s2[i] = s1[i]) != '##代码##') i++; }

回答by xagyg

You can simulate EOF with CTRL+D(for *nix) or CTRL+Z(for Windows) from command line.

您可以从命令行使用CTRL+D(for *nix) 或CTRL+Z(for Windows)模拟 EOF 。

回答by mauriceShun

In widows, when you are ready to complete the input, press the Enterkey and then press Ctrl+Zand then Enterto complete the input.

在widows中,当您准备好完成输入时,按Enter键再按Ctrl+Z然后Enter完成输入。

##代码##

回答by crajun

In the end, it can't be done easily on Windows given the simple K&R code which was meant for Unix-like systems. You can send '^Z^M' (Ctrl-Z and then Enter) to send Windows equivalent of EOF but the char 'EOF' you are checking for in this C program is not the same.

最后,鉴于适用于类 Unix 系统的简单 K&R 代码,在 Windows 上无法轻松完成。您可以发送 '^Z^M'(Ctrl-Z 然后回车)来发送等效于 EOF 的 Windows,但是您在这个 C 程序中检查的字符 'EOF' 是不一样的。

Short answer: you can't.

简短的回答:你不能。