'\b' problem while redirecting I/O to and from files

'\b' problem while redirecting I/O to and from files

Post by David Pinkert » Thu, 08 Oct 1998 04:00:00



I'm fairly new to C and was working on a program to remove comments
from C code (just an exercise).  This uses a while((getchar())!=EOF)
loop to read characters.  Basically when it sees a character inside a
comment, I am using putchar('\b'); putchar(' '); to remove the
character.  When I physically type in each line from the keyboard, it
works fine.  However if I do it this way:

program < input.c > output_file

I get ^H instead of \b (even though stty erase is set to ^?).  The
backspace key works fine otherwise.  Is there something else I'm
missing?  The strange part is that it works exactly the same was in
DOS.  I know it's because of the I/O redirection from files but I'm
not sure why.  Any clues would be very much appreciated!  
I did a Dejanews search but all I could find where problems with
people not having their backspace key set correctly.
I don't think it makes any difference but I'm using Debian Linux.

David

 
 
 

'\b' problem while redirecting I/O to and from files

Post by Barry Margoli » Fri, 09 Oct 1998 04:00:00




>I'm fairly new to C and was working on a program to remove comments
>from C code (just an exercise).  This uses a while((getchar())!=EOF)
>loop to read characters.  Basically when it sees a character inside a
>comment, I am using putchar('\b'); putchar(' '); to remove the
>character.  When I physically type in each line from the keyboard, it
>works fine.  However if I do it this way:

>program < input.c > output_file

>I get ^H instead of \b (even though stty erase is set to ^?).  The

You're putting a backspace in the file.  Programs that display control
characters literally use ^H to display backspace.  "Stty erase" merely
controls what you type to delete a character during input editing, it has
nothing to do with output.

Quote:>backspace key works fine otherwise.  Is there something else I'm
>missing?  The strange part is that it works exactly the same was in
>DOS.  I know it's because of the I/O redirection from files but I'm
>not sure why.  Any clues would be very much appreciated!  

Outputting backspace followed by space erases a character on the screen by
overwriting it with a space.  But when you put this in a file, it still
contains the original character followed by the space.

--

GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.

 
 
 

'\b' problem while redirecting I/O to and from files

Post by cra.. » Sat, 10 Oct 1998 04:00:00



Quote:> I'm fairly new to C and was working on a program to remove comments
> from C code (just an exercise).  This uses a while((getchar())!=EOF)
> loop to read characters.  Basically when it sees a character inside a
> comment, I am using putchar('\b'); putchar(' '); to remove the

To answer your question, '\b' only has special significance to the
terminal driver, which takes care of receiving input from the keyboard.
Copying a '\b' from a file to the screen doesn't do what you expected.

As far as your program goes, perhaps not copying comments to output
would be simpler.

A program that writes C programs like you're writing is flex(1), and it's
man page has an example that removes C comments.

Craig