什么\\ t \\ B就做什么? [英] What do \t and \b do?

查看:123
本文介绍了什么\\ t \\ B就做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这个简单的code的行

I expect this simple line of code

printf("foo\b\tbar\n");

更换O与\\ t和产生以下输出

to replace "o" with "\t" and to produce the following output

fo     bar

(假设制表位每8个字符,则会出现)。
相反,我得到

(assuming that tab stop occurs every 8 characters). On the contrary I get

foo    bar

看来,我的壳间$ P $点\\ b以移动光标一个位置后退和\\ T作为将光标移动到下一个制表位。这种行为是特定于我运行code壳?我应该期待在不同的系统不同的行为?

It seems that my shell interprets \b as "move the cursors one position back" and \t as "move cursor to the next tab stop". Is this behaviour specific to the shell in which I'm running the code? Should I expect different behaviour on different systems?

推荐答案

退格键和标签都移动光标位置。无论是不折不扣的打印字符。

Backspace and tab both move the cursor position. Neither is truly a 'printable' character.

您code说:


  1. 打印富

  2. 将光标回一个空格

  3. 向前移动光标到下一个制表位

  4. 输出栏。

要得到你所期望的输出,需要的printf(富\\ B \\ TBAR)。注意额外的空间。上面写着:

To get the output you expect, you need printf("foo\b \tbar"). Note the extra 'space'. That says:


  1. 输出富

  2. 将光标回一个空格

  3. 输出'(这取代了第​​二个O)。

  4. 向前移动光标到下一个制表位

  5. 输出栏。

在大多数情况下是不恰当的使用选项卡和退格格式化你的程序的输出。了解如何使用的printf()格式说明符。标签的渲染可以根据对输出的观察变化显着。

Most of the time it is inappropriate to use tabs and backspace for formatting your program output. Learn to use printf() formatting specifiers. Rendering of tabs can vary drastically depending on how the output is viewed.

本小脚本显示改变终端的标签渲染的一种方式。经测试+ Ubuntu的Gnome终端上:

This little script shows one way to alter your terminal's tab rendering. Tested on Ubuntu + gnome-terminal:

#!/bin/bash
tabs -8 
echo -e "\tnormal tabstop"
for x in `seq 2 10`; do
  tabs $x
  echo -e "\ttabstop=$x"
 done

tabs -8
echo -e "\tnormal tabstop"

另见男人setterm这个 regtabs

如果你重定向你的输出或者只写一个文件,标签将相当普遍显示为比标准的8个字符少,尤其是在规划编辑器和IDE。

And if you redirect your output or just write to a file, tabs will quite commonly be displayed as fewer than the standard 8 chars, especially in "programming" editors and IDEs.

因此​​,在otherwords:

So in otherwords:

printf("%-8s%s", "foo", "bar"); /* this will ALWAYS output "foo     bar" */
printf("foo\tbar"); /* who knows how this will be rendered */

恕我直言,在一般的标签是很少适合什么。一个例外可能会产生一个程序的输出,需要制表符分隔值输入文件(类似于逗号分隔值)。

IMHO, tabs in general are rarely appropriate for anything. An exception might be generating output for a program that requires tab-separated-value input files (similar to comma separated value).

退格键'\\ B'是一个不同的故事......它不应该被用来创建一个文本文件,因为这将只是做一个文本编辑器吐出垃圾。但它确实有很多应用程序以书面形式不能单独格式字符串来实现交互式命令行程序。如果你发现自己需要它了,检查了ncurses的,它使您可以在您的输出将在终端屏幕上更好的控制。并且一般,因为它是2011年不是1995年,一个GUI通常是容易对付的高度互动节目。但同样,也有例外。就像写一个telnet服务器或控制台的一个新的脚本语言。

Backspace '\b' is a different story... it should never be used to create a text file since it will just make a text editor spit out garbage. But it does have many applications in writing interactive command line programs that cannot be accomplished with format strings alone. If you find yourself needing it a lot, check out "ncurses", which gives you much better control over where your output goes on the terminal screen. And typically, since it's 2011 and not 1995, a GUI is usually easier to deal with for highly interactive programs. But again, there are exceptions. Like writing a telnet server or console for a new scripting language.

这篇关于什么\\ t \\ B就做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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