help with a program

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • broli

    help with a program

    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 ?
  • Richard Heathfield

    #2
    Re: help with a program

    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.dat a", "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

    Comment

    • WANG Cong

      #3
      Re: help with a program

      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.

      Comment

      • broli

        #4
        Re: help with a program

        On Mar 14, 1:35 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
        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.dat a", "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!

        Comment

        • Flash Gordon

          #5
          Re: help with a program

          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

          Comment

          Working...