stdin在命令提示符下输入问题 [英] stdin input problems on command prompt

查看:130
本文介绍了stdin在命令提示符下输入问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在命令窗口创建了一个简单的提示,它接受任何输入的字符串并对其进行标记,然后将其显示回来。如果键入exit,程序将退出。



我遇到的麻烦是在命令提示符下,程序运行时,如果我完全移动箭头键,我可以进一步编辑打印文本命令提示符,以及程序之前的任何内容。如果我然后去底部并输入一些文本程序搞砸了并显示一些奇怪的输入。



我的代码如下,任何帮助将不胜感激。我有一种感觉我需要以某种方式关闭stdin。



I have created a simple prompt on the command window, that takes any entered string and tokenized it, then displayed it back. If "exit" is typed the program exits.

The trouble I'm having is that within the command prompt, while the program runs, if I move the arrow keys at all I can edit printed text further up the command prompt, as well as anything before the program. If i then go to the bottom and enter some text the program messes up and displays some weird input.

My code is below, and any help would be appreciated. I have a feeling I need to close the stdin somehow.

#define INPUT_SIZE 513

#define FLUSH_STDIN(x) {if(x[strlen(x)-1]!='\n'){ do fgets(Junk,100,stdin); while(Junk[strlen(Junk)-1]!='\n');}else x[strlen(x)-1]='\0';}

char Junk[100]; // buffer for discarding excessive user input, 
               // used by "FLUSH_STDIN" macro
			   
void getInput(char *user_input){

printf(">>: ");
fgets(user_input,INPUT_SIZE,stdin);
}

void removeNewLine(char *str){
int len = strlen(str);
			if (str[len-1] == '\n')
				str[len-1] = 0;

}

int main(int argc, char *argv[]) {

char *user_input;
char *cur_token;
const char delim[2] = " ";
int session = 1;

while (session != 0)
{
	getInput(user_input);
	removeNewLine(user_input);
	
	int len = strlen(user_input);
	if (strcmp(user_input,"exit") ==0)
		session = 0;
	else{
		printf("Entered string split into: ");
		cur_token = strtok(user_input, delim);
		while(cur_token != NULL){
			printf("\"%s\"  ",cur_token);
			cur_token = strtok(NULL, delim);
		}
		if (!(len < (INPUT_SIZE-1)))
			FLUSH_STDIN(user_input);
		
		printf("\n");
	}
} 

return(0);
}

推荐答案

您有经验丰富的人提供了一些经过深思熟虑的回复。



如果您阅读此 http://www.cplusplus .com / reference / cstdio / fgets / [ ^ ]你可能会理解为何提出意见。



目前代码
You have some considered responses from experienced people.

If you read this http://www.cplusplus.com/reference/cstdio/fgets/[^] you may understand why the comments were made.

At the moment in your code
user_input

指向太空。在使用它之前,你需要为它分配一些内存

points into space. You need to allocate some memory to it before you use it with

fgets()

你现在得到一些事情的事实只是巧合。这个bug需要先修复。



你还没有说你正在使用哪个编译器和操作系统会影响你的结果。



一些解释你的代码意图的意见也是一个好主意。



我建议这些更改:



The fact that you get something to happen at the moment is just coincidence. That bug needs to be fixed first.

You haven't said which compiler and OS you are using which will affect your results.

A few comments explaining what your code is meant to do would also be a good idea.

I would suggest these changes:

char user_input[INPUT_SIZE ];
char *cur_token=NULL;





更改此功能:



Change this function:

char * getInput(char *user_input){

printf(">>: ");
return fgets(user_input,INPUT_SIZE,stdin);
}





并加上:





And add this:

if(NULL ==getInput(user_input))
    {
    printf("Input error occured.\n");
    break;
}







目前FLUSH_STDIN永远不会被调用(甚至在你做任何事情之前)变化):






At the moment FLUSH_STDIN can never be called (even before you make any changes):

if (!(len < (INPUT_SIZE-1)))
    FLUSH_STDIN(user_input);





为什么你使用FLUSH_STDIN(x)的宏而不是函数? http://stackoverflow.com/questions/14041453/为什么是预处理器 - 宏 - 邪恶 - 什么是替代品 [ ^ ]



因为您的应用程序正在运行在控制台中,您可以访问命令行历史记录。向上和向下箭头用于查看此内容。可以修改此行为:

http: //msdn.microsoft.com/en-us/library/ms686031%28v=VS.85%29.aspx [ ^ ]

http://msdn.microsoft.com/en-us/library/ms682077%28v=VS.85%29.aspx [ ^ ]



在Windows 7中,如果按F7,您将看到整个历史记录。

http://en.kioskea.net/faq/14204-windows-7-view-the-命令提示历史 [ ^ ]



alt-F 7将清除历史。您也可以手动更改默认设置。单击控制台的控制框(左上角),属性,选项选项卡:命令历史记录。默认值为50个项目,4个缓冲区。



Why have you used a macro for FLUSH_STDIN(x) and not a function? http://stackoverflow.com/questions/14041453/why-are-preprocessor-macros-evil-and-what-are-the-alternatives[^]

Because your application is running in a console you have access to command line history. The up and down arrows are used to view this. This behaviour can be modified:
http://msdn.microsoft.com/en-us/library/ms686031%28v=VS.85%29.aspx[^]
http://msdn.microsoft.com/en-us/library/ms682077%28v=VS.85%29.aspx[^]

In Windows 7 if you press F7 you will see the entire history.
http://en.kioskea.net/faq/14204-windows-7-view-the-command-prompt-history[^]

alt-F7 will clear the history. You may also change the default settings manually. Click the console's control box (top left), properties, options tab: "Command history." The default is 50 items, 4 buffers.


这篇关于stdin在命令提示符下输入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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