使用scanf时如何绕过打印新行?

时间:2022-09-06 18:46:19

Is there any way to do this?.

有没有办法做到这一点?

Lets take this code as an example:

让我们以此代码为例:

int num;
printf("enter a number: ");
scanf("%d",&num);
printf("<- this is your number.");

Output will be like this:

输出将是这样的:

enter a number: 2
<- this is your number.

What I want it to be:

我想要的是:

enter a number: 2<-this is your number.

4 个解决方案

#1


You can't do this with scanf. Depending on your platform(linux, windows, ...) you should use the library ncurses or similar.

你不能用scanf做到这一点。根据您的平台(linux,windows,...),您应该使用库ncurses或类似的。

#2


thank you guys for the help. after doing some searching find this:

谢谢你们的帮助。做了一些搜索后发现这个:

int num;
printf("Enter a number: ");
scanf("%d", &num);

CONSOLE_SCREEN_BUFFER_INFO coninfo;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &coninfo);
coninfo.dwCursorPosition.Y -= 1;    
coninfo.dwCursorPosition.X += 20;    
SetConsoleCursorPosition(hConsole, coninfo.dwCursorPosition);

printf("<- this is your number.");

output: enter a number. 2 <- this is your number.

输出:输入一个数字。 2 < - 这是你的电话号码。

#3


I am assuming what you are seeing in your terminal when running the application is stdin being echoed.

在运行应用程序时,我假设您在终端中看到的是stdin被回显。

What that means is that stdin is going to echo what it receives to stdout.

这意味着stdin将回应它收到的stdout。

As Chux mentions above this is platform dependent (and terminal application dependent) behavior.

正如Chux在上面提到的,这是依赖于平台的(和终端应用程序相关的)行为。

Good information here:

这里有好消息:

1.Hide password input on terminal 2. echoing of getchar and '\n' char from stdin

1.终端上隐藏密码输入2.来自stdin的getchar和'\ n'字符的回显

#4


There is no portable way to do that, because by default input is line buffered and echoed at low level in current platforms (at least Windows and Unix-like systems).

没有可移植的方法,因为默认输入是行缓冲并在当前平台(至少Windows和类Unix系统)中以低级别回显。

There are ways not to use line buffered input or to use manual echo, but it will be platform dependent.

有些方法可以不使用行缓冲输入或使用手动回声,但它将取决于平台。

Under Linux for example, you could put input in non canonical mode (see man termios) : that would allow you to read data one char at a time (for example) with echo off, do the echo manually, do not echo the newline but process the input buffer (with sscanf) after a newline.

例如,在Linux下,您可以将输入置于非规范模式(请参阅man termios):这将允许您一次读取一个char(例如),使用echo off,手动执行echo,不回显换行但是在换行符后处理输入缓冲区(使用sscanf)。

Under Windows, you could use the function getch (declared in conio.h) to also get one character at a time without echo. And I have no idea on how to do that on a Mac.

在Windows下,您可以使用函数getch(在conio.h中声明)同时获取一个没有echo的字符。我不知道如何在Mac上这样做。

#1


You can't do this with scanf. Depending on your platform(linux, windows, ...) you should use the library ncurses or similar.

你不能用scanf做到这一点。根据您的平台(linux,windows,...),您应该使用库ncurses或类似的。

#2


thank you guys for the help. after doing some searching find this:

谢谢你们的帮助。做了一些搜索后发现这个:

int num;
printf("Enter a number: ");
scanf("%d", &num);

CONSOLE_SCREEN_BUFFER_INFO coninfo;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &coninfo);
coninfo.dwCursorPosition.Y -= 1;    
coninfo.dwCursorPosition.X += 20;    
SetConsoleCursorPosition(hConsole, coninfo.dwCursorPosition);

printf("<- this is your number.");

output: enter a number. 2 <- this is your number.

输出:输入一个数字。 2 < - 这是你的电话号码。

#3


I am assuming what you are seeing in your terminal when running the application is stdin being echoed.

在运行应用程序时,我假设您在终端中看到的是stdin被回显。

What that means is that stdin is going to echo what it receives to stdout.

这意味着stdin将回应它收到的stdout。

As Chux mentions above this is platform dependent (and terminal application dependent) behavior.

正如Chux在上面提到的,这是依赖于平台的(和终端应用程序相关的)行为。

Good information here:

这里有好消息:

1.Hide password input on terminal 2. echoing of getchar and '\n' char from stdin

1.终端上隐藏密码输入2.来自stdin的getchar和'\ n'字符的回显

#4


There is no portable way to do that, because by default input is line buffered and echoed at low level in current platforms (at least Windows and Unix-like systems).

没有可移植的方法,因为默认输入是行缓冲并在当前平台(至少Windows和类Unix系统)中以低级别回显。

There are ways not to use line buffered input or to use manual echo, but it will be platform dependent.

有些方法可以不使用行缓冲输入或使用手动回声,但它将取决于平台。

Under Linux for example, you could put input in non canonical mode (see man termios) : that would allow you to read data one char at a time (for example) with echo off, do the echo manually, do not echo the newline but process the input buffer (with sscanf) after a newline.

例如,在Linux下,您可以将输入置于非规范模式(请参阅man termios):这将允许您一次读取一个char(例如),使用echo off,手动执行echo,不回显换行但是在换行符后处理输入缓冲区(使用sscanf)。

Under Windows, you could use the function getch (declared in conio.h) to also get one character at a time without echo. And I have no idea on how to do that on a Mac.

在Windows下,您可以使用函数getch(在conio.h中声明)同时获取一个没有echo的字符。我不知道如何在Mac上这样做。