了解退格(\\ B)用C行为 [英] Understand backspace (\b) behaviour in C

查看:278
本文介绍了了解退格(\\ B)用C行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个程序其输入复制到其输出,更换标签,按 \\ t )\\ t 退格键( \\ b )通过 \\ b
但是在这里我的code,我不能当我输入退格键它不是替换为制表作品读取输入的字符。

This program copy its input to its output, replacing TAB(\t) by \t backspace(\b) by \b. But here in my code I am unable to read input character when I enter backspace its not replacing as a tab works .

在Linux下用gcc编译:

Compiling with GCC in Linux:

#include<stdio.h>
int main(void)
{
    int c=0;
    while((c=getchar())!=EOF){
     if(c=='\t'){
      printf("\\t");
     if(c=='\b')
      printf("\\b");
    }
    else
     putchar(c); 
}
return 0;
}

假设,如果我输入维奈(标签)hunachyal

Suppose if I type vinay (tab) hunachyal

Output:vinay\thunachyal 

如果我输入维奈(1退格键)

If I type vinay(and 1 backspace)

Output:vina

所以我的查询是为什么维娜\\ b 是不是在这种情况下打印?结果
是否有可能检测到 \\ b 和打印 \\ b ?如果没有什么理由

So my query is why vina\b is not printing in this case?
Is it possible to detect \b and print \b? if not whats the reason

注:我需要在运行时输入退格没有提供单独的文件中有\\ b

Note: I need at run time input backspace not providing separate file having \b

推荐答案

退格是由外壳间preTER消耗掉,所以你的程序将永远不会看到它,也是你的code是(略)破,由于错位括号,而不是由劣质压痕帮助。

The backspace is consumed by the shell interpreter, so your program will never see it, also your code is (slightly) broken, due to misplaced braces, not helped by the poor indentation.

下面是修正版本:

#include<stdio.h>
int main(void)
{
    int c=0;
    while((c=getchar())!=EOF){
        if(c=='\t')
            printf("\\t");
        else if(c=='\b')
            printf("\\b");
        else
            putchar(c);
    }
    putchar('\n');
    return 0;
}

这按预期工作:

$ echo 'vinay\thunachyal\b' | ./escape
vinay\thunachyal\b

这篇关于了解退格(\\ B)用C行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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