help with a program

Discussion in 'C Programming' started by broli, Mar 14, 2008.

  1. broli

    broli Guest

    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 ?
    broli, Mar 14, 2008
    #1
    1. Advertising

  2. broli said:

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


    <snip>

    > 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
    Richard Heathfield, Mar 14, 2008
    #2
    1. Advertising

  3. broli

    broli Guest

    On Mar 14, 1:35 pm, Richard Heathfield <> wrote:
    > broli said:
    >
    > > This appears on page number 20 of K & R 2 exercise number 1-10

    >
    > <snip>
    >
    > > 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!
    broli, Mar 14, 2008
    #3
  4. broli

    WANG Cong Guest

    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?


    --
    Hi, I'm a .signature virus, please copy/paste me to help me spread
    all over the world.
    WANG Cong, Mar 14, 2008
    #4
  5. Richard Heathfield <> writes:
    > broli said:
    >
    >> This appears on page number 20 of K & R 2 exercise number 1-10

    >
    > <snip>
    >
    >> 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.

    [...]

    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.

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Mar 14, 2008
    #5
  6. broli

    Flash Gordon Guest

    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 <stdio.h>

    > int main(void)
    > {
    > int c;


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

    > char *s;


    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.

    > }

    --
    Flash Gordon
    Flash Gordon, Mar 14, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pipi
    Replies:
    1
    Views:
    617
    Joseph Millar
    Jul 21, 2003
  2. Rey
    Replies:
    4
    Views:
    10,934
    Roedy Green
    Dec 12, 2003
  3. ola
    Replies:
    3
    Views:
    710
    Marco Schmidt
    Feb 16, 2004
  4. Replies:
    1
    Views:
    469
    Walter Roberson
    Aug 12, 2005
  5. Kris Stark
    Replies:
    1
    Views:
    96
    John Bokma
    Sep 8, 2005
Loading...

Share This Page