This simple program acquires characters from the users (till a newline character) and prints them:
这个简单的程序从用户那里获得字符(直到换行符)并打印出来:
#include <stdio.h>
int main()
{
char local_message[200];
scanf("%[^\n]%*c", local_message);
printf("Your message is %s\n", local_message);
return 0;
}
But when I run it, the characters I typed stay printed into the screen:
但是当我运行它时,我输入的字符会被打印到屏幕上:
Hello World!
Your message is Hello World!
The characters I typed were "Hello World!\n". I would like that when I press "Enter" to create the newline \n
character, my message disappears from the screen (while being stored into the char
array), so that I can print (and format) it with the following printf
.
我输入的字符是“Hello World!\n”。我希望当我按下“Enter”来创建newline \n字符时,我的消息将从屏幕上消失(同时存储到char数组中),以便我可以用以下printf打印(并格式化)它。
The OS is Linux.
Linux操作系统。
Is it possible? How?
是可能的吗?如何?
1 个解决方案
#1
4
You may be able to do this with terminal escape codes.
您可以使用终端转义代码来实现这一点。
#include <stdio.h>
int main ( ) {
char local_message[200];
scanf("%199[^\n]%*c", local_message);
printf ( "\033[0A");//move cursor up one line
printf ( "\033[2K");//clear line
printf("\nYour message is %s\n", local_message);
return 0;
}
EDIT:
This will give a little more control and erase inputs more than one line.
编辑:这将提供更多的控制,并删除多行输入。
#include <stdio.h>
int main ( ) {
char local_message[200];
printf ( "\033[2J");//clear screen and move cursor to upper left corner
printf ( "\033[8;H");//move cursor to line 8
printf("Enter Your message\n");
scanf("%199[^\n]%*c", local_message);
printf ( "\033[9;H");//move cursor to line 9
printf ( "\033[J");//clear screen from line to end
printf ( "\033[12;H");//move cursor to line 12
printf("\nYour message is %s\n", local_message);
return 0;
}
#1
4
You may be able to do this with terminal escape codes.
您可以使用终端转义代码来实现这一点。
#include <stdio.h>
int main ( ) {
char local_message[200];
scanf("%199[^\n]%*c", local_message);
printf ( "\033[0A");//move cursor up one line
printf ( "\033[2K");//clear line
printf("\nYour message is %s\n", local_message);
return 0;
}
EDIT:
This will give a little more control and erase inputs more than one line.
编辑:这将提供更多的控制,并删除多行输入。
#include <stdio.h>
int main ( ) {
char local_message[200];
printf ( "\033[2J");//clear screen and move cursor to upper left corner
printf ( "\033[8;H");//move cursor to line 8
printf("Enter Your message\n");
scanf("%199[^\n]%*c", local_message);
printf ( "\033[9;H");//move cursor to line 9
printf ( "\033[J");//clear screen from line to end
printf ( "\033[12;H");//move cursor to line 12
printf("\nYour message is %s\n", local_message);
return 0;
}