help with a program

B

broli

This appears on page number 20 of K & R 2 exercise number 1-10

Q. Write a program to copy its input to its output, replacing each tab
by \t, each backspace by \b, and each backslash by \\. This makes the
tabs and backspaces visible in an unambiguous way.


Here's my attempt :

#include<stdio.h>

int main(void)
{
int c;
char *s;

while( (c=getchar())!=EOF)
{
if( c == '\b' || c == '\t' || c == '\\')
{
if(c =='\b')
{
s = "\\b";
printf("%s",s);
}

if(c == '\t')
{
s = "\\t";
printf("%s",s);
}

if(c == '\\')
{
s = "\\\\";
printf("%s",s);
}
}

else
printf("%c",c);

}

return 0;
}

The program compiles successfully but when I execute the program it
seems that it can't handle backspaces( '\b' character). The program
works fine when it encounters thet tabs and backslashes. Can some one
please tell me what is wrong here ?
 
R

Richard Heathfield

broli said:
This appears on page number 20 of K & R 2 exercise number 1-10

The program compiles successfully but when I execute the program it
seems that it can't handle backspaces( '\b' character). The program
works fine when it encounters thet tabs and backslashes. Can some one
please tell me what is wrong here ?

Nothing!

The problem is that your shell is interpreting the backspace for you - so
the program never gets to see it. But you can still test the code easily
enough, by writing a file containing backspaces, and piping that file
through your code.

Here is a little program to generate a test file:

#include <stdio.h>

int main(void)
{
char linea[] = "Now is the time for all f\bgood men";
char lineb[] = "to come to the b\baid of their party.";
FILE *fp = fopen("test.data", "w");
if(fp != NULL)
{
fprintf(fp, "%s\n", linea);
fprintf(fp, "%s\n", lineb);
if(ferror(fp))
{
fprintf(stderr, "Problem writing file.\n");
}
if(fclose(fp))
{
fprintf(stderr, "Problem closing file.\n");
}
}
else
{
fprintf(stderr, "Problem opening file.\n");
}
return 0;
}

Compile and run the program, and it will create a file called test.data,
containing two backspaces.

Run your program from a shell, and redirect the input from test.data:

Windows:
yourprogramname < test.data

Linux:
../yourprogramname < test.data

I've tested your program on my machine using this method, and it handles
backspaces just fine.
 
B

broli

broli said:
This appears on page number 20 of K & R 2 exercise number 1-10

The program compiles successfully but when I execute the program it
seems that it can't handle backspaces( '\b' character). The program
works fine when it encounters thet tabs and backslashes. Can some one
please tell me what is wrong here ?

Nothing!

The problem is that your shell is interpreting the backspace for you - so
the program never gets to see it. But you can still test the code easily
enough, by writing a file containing backspaces, and piping that file
through your code.

Here is a little program to generate a test file:

#include <stdio.h>

int main(void)
{
char linea[] = "Now is the time for all f\bgood men";
char lineb[] = "to come to the b\baid of their party.";
FILE *fp = fopen("test.data", "w");
if(fp != NULL)
{
fprintf(fp, "%s\n", linea);
fprintf(fp, "%s\n", lineb);
if(ferror(fp))
{
fprintf(stderr, "Problem writing file.\n");
}
if(fclose(fp))
{
fprintf(stderr, "Problem closing file.\n");
}
}
else
{
fprintf(stderr, "Problem opening file.\n");
}
return 0;

}

Compile and run the program, and it will create a file called test.data,
containing two backspaces.

Run your program from a shell, and redirect the input from test.data:

Windows:
yourprogramname < test.data

Linux:
./yourprogramname < test.data

I've tested your program on my machine using this method, and it handles
backspaces just fine.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Thank you!
 
W

WANG Cong

On Fri, 14 Mar 2008 01:18:44 -0700,broli wrote:
This appears on page number 20 of K & R 2 exercise number 1-10

Q. Write a program to copy its input to its output, replacing each tab
by \t, each backspace by \b, and each backslash by \\. This makes the
tabs and backspaces visible in an unambiguous way.


Here's my attempt :

#include<stdio.h>

int main(void)
{
int c;
char *s;

while( (c=getchar())!=EOF)
{
if( c == '\b' || c == '\t' || c == '\\')
{
if(c =='\b')
{
s = "\\b";
printf("%s",s);
}

if(c == '\t')
{
s = "\\t";
printf("%s",s);
}

if(c == '\\')
{
s = "\\\\";
printf("%s",s);
}
}

else
printf("%c",c);

}

return 0;
}

The program compiles successfully but when I execute the program it
seems that it can't handle backspaces( '\b' character). The program
works fine when it encounters thet tabs and backslashes. Can some one
please tell me what is wrong here ?

Your program works fine here. What wrong with it there?
 
K

Keith Thompson

Richard Heathfield said:
broli said:




Nothing!

The problem is that your shell is interpreting the backspace for you - so
the program never gets to see it. But you can still test the code easily
enough, by writing a file containing backspaces, and piping that file
through your code.
[...]

A small system-specific quibble: It's probably not the shell that's
interpreting the backspaces. When the program is reading input, the
shell isn't even running; it's suspended, waiting for the program to
terminate. On a Unix-like system, it's the terminal code (there's
probably a more accurate term for it) that processes certain control
characters, the ways in which it does so can be affected by the "stty"
command.

But it's pretty much the same idea.

One feature of the tty handling on Unix-like system is that there's
generally a way to pass through literal control characters. By
default, typing control-V causes the next character to be interpreted
literally. This might not work in all circumstances, and is unlikely
to work on non-Unix-like systems, but where it does work it's a good
way to exercise this program.
 
F

Flash Gordon

broli wrote, On 14/03/08 08:18:

<snip>

Others have answered your question, but I've not seen any comments on
your code. So I will point out some things which can be improved.
#include<stdio.h>

A lot of people would find it more readable if you used a space.
#include said:
int main(void)
{
int c;

The above is good. You have avoided some common errors.

See comments below for why you don't need s.
while( (c=getchar())!=EOF)

This is good, you have avoided more common errors.
{
if( c == '\b' || c == '\t' || c == '\\')
{
if(c =='\b')
{
s = "\\b";
printf("%s",s);
}

if(c == '\t')

You know about else, so why did you not use it? If you did...
{
s = "\\t";
printf("%s",s);
}

if(c == '\\')
{
s = "\\\\";
printf("%s",s);
}
}

The above could all be rewritten as
if (c == '\b')
printf("\b");
else if (c == '\t')
printf("\t");
...
else
printf("%c",c);

In fact, there is a function putchar which would be even better, since
you just want to sent one character to the output.

Finally, rather than a chain of if statements you could use a single switch.
}

return 0;

Good, that is something a number of beginners (and even people who have
been programming for longer) miss.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top