如何使用\\ b,来删除一个换行符 [英] How to delete a newline using \b

查看:225
本文介绍了如何使用\\ b,来删除一个换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想下面的code的输出


  

- | -2- | -


(当i输入作为2键的值)。我知道\\ b只是将光标移动到1空间回来,但为什么它是不能够移动一个空格后面一个换行符用于输入后键的值。是否有可能使用转义字符\\ B A换行符已被使用了。

 #包括LT&;&stdio.h中GT;
#包括LT&;&CONIO.H GT;诠释的main()
{
    INT关键;
    的printf(回车键的值:)
    的printf( - | - );
    scanf函数(%d个,&安培;键);
    的printf(\\ B超| - );
    返回0;
}


解决方案

您使用的是作为输出设备的终端不具备文本行中的任何概念:所有它知道的是单元网格,每个都可以由一个字符来填充。一个细胞的位置是在当前小区

的端子间$ P $点则BS字符作为指令来一一列移到当前小区到左侧。但是,如果你已经在第一列中会发生什么?终端不知道previous行结束的地方,所以也不能回到该行的末尾。它可以回到上一行的最后一列,而一些终端或许可以被配置为做到这一点,但是这不太可能你想要的。

这是过于简单化,但它基本上是为什么 \\ b 不会删除换行符。

I want the output of the following code as

-|-2-|-

(When i input the value of key as 2). I know \b just move the cursor to 1 space back but why it isn't able to move one space back after a newline is used to input the value of key. Is it possible to use the escape character \b after a newline character has been used.

#include<stdio.h>
#include<conio.h>

int main()
{
    int key;
    printf("Enter value of key: )"
    printf("-|-");
    scanf("%d",&key);
    printf("\b-|-");
    return 0;
}

解决方案

The terminal you are using as an output device does not have any concept of lines of text: all it knows about is a grid of cells, each of which can be filled by a character. One of the cell positions is the current cell.

The terminal interprets the BS character as an instruction to move the current cell one one column to the left. But what happens if you are already in the first column? The terminal doesn't know where the previous line ended, so it can't go back to the end of the line. It could go back to the last column of the row above, and some terminals can probably be configured to do that, but that's not likely what you want.

This is overly simplified, but it's basically why \b won't delete a line break.

这篇关于如何使用\\ b,来删除一个换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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