[英]Why does my C program close early?
I am having a very confusing problem right now. 我现在遇到一个非常令人困惑的问题。 I wrote a test program for myself, but sometimes after I input Crtl+C, which is what I assume to be the EOF, the program closes early without running through some more commands below the While Loop I wanted it to cancel from.
我为自己编写了一个测试程序,但是有时在我输入Crtl + C(我认为是EOF)后,该程序会提前关闭,而无需在我希望其取消的While循环下运行其他命令。
#include <stdio.h>
#include <string.h>
int main()
{
char message[140];
char* p = message;
int count;
int i = 0;
int charGT;
while((charGT=getchar()) != EOF)
{
message[i] = charGT;
i++;
printf("%d" , i);
}
printf("next");
count = strlen(p);
printf("%d", count);
printf("after");
return (0);
}
Inputting "asd\\n^C" will cause my program to end early. 输入“ asd \\ n ^ C”将导致我的程序提前结束。 "next" or "after" will not be printed My theory is that somehow, the \\n is causing the program to step out of the loop for a moment, and then Crtl+C is exiting the program, but I don't know how that would work.
我不会说“ next”或“ after”。我的理论是,\\ n使程序暂时退出循环,然后Crtl + C退出程序,但我不知道那将如何工作。
Ctrl + C is the break command and will send an interrupt signal to your application. Ctrl + C是break命令,它将向您的应用程序发送一个中断信号。 The default handler will cause this to exit the process.
默认处理程序将导致此操作退出流程。
Ctrl + D is the EOF command you are looking for. Ctrl + D是您要查找的EOF命令。
For running on windows, to enter EOF press Ctrl+Z and then press ENTER. 要在Windows上运行,要输入EOF,请按Ctrl + Z ,然后按Enter。 In UNIX systems it is Ctrl+D, in Windows Ctrl+Z.
在UNIX系统中,它是Ctrl + D,在Windows中是Ctrl + Z。
When a program is running if you press Ctrl-c a signal is sent to abort the program and the program is aborted. 当程序运行时,如果您按Ctrl-c,则会发送信号以中止该程序,并且该程序也将中止。 However, if you want to use Ctrl-c as an input but not abort the program write one signal handler which catches the signal when you press Ctrl-c and perform the action you wanted to.
但是,如果要使用Ctrl-c作为输入但不中止程序,请编写一个信号处理程序,该信号处理程序在按Ctrl-c并执行所需的操作时会捕获信号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.