intercepting backspaces

This is a discussion on intercepting backspaces within the C Programming forums, part of the General Programming Boards category; Hi. This is a new thread on an old subject. I do this so i do not bump an old ...

  1. #1
    C Beginner()
    Join Date
    Oct 2004
    Posts
    22

    intercepting backspaces

    Hi. This is a new thread on an old subject. I do this so i do not bump an old thread anymore.

    Sorry, kermit.

    From seeing the posts before mine this question came into my mind: why would Kernighan & Ritchie put exercise 1-10 at that point of their book if the "\b part" of it would not work? May it be that it worked in the computers/OSs they used when they wrote the book?

    Put in another way, why should it be necessary to flush standard input in order to get the program to do all the tasks it is told to do when those concepts were not discussed at that point in the book?

    Read correctly, i'm grateful to all of you who shared your knowledge but i think that it is important to answer these kind of questions with the knowledge you got by reading up to that part of the book. Sometimes, invoking functions that were not discussed might confuse newbies (such as myself.)

    Thanks to all, great forum.

  2. #2
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    K&R is a programmer's reference, not a textbook.

    The reason for the fflush(stdin) is that Unix (and its work-alikes), by default, buffer keyboard input. If you're using scanf() to get input from a tty (such as stdin), and you don't provide for line termination ('\n') at the end of your scanf() "format" string, you end up with the trailing stuff (the newline) in the buffer, and the next scanf() reads that as the end-of-line and doesn't read in stuff for the variables in that call. (Always check the return value of scanf().)

    I've not read your other thread, but I hope this helps answer your questions.
    Insert obnoxious but pithy remark here

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    Who said anything about flushing stdin - something which you can't do anyway.

    All exercise 1-10 needs is getchar(), printf() and some if statements, all of which have just been described in the previous paragraphs.

    The line buffered nature of stdin means you're not likely to see a \b when typing in at the keyboard, since the terminal driver usually resolves that before passing the whole line to your program.

  4. #4
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by Salem
    Who said anything about flushing stdin - something which you can't do anyway.

    All exercise 1-10 needs is getchar(), printf() and some if statements, all of which have just been described in the previous paragraphs.

    The line buffered nature of stdin means you're not likely to see a \b when typing in at the keyboard, since the terminal driver usually resolves that before passing the whole line to your program.

    Very true..... but also:

    I'm going way back here, but considering the first machine I programmed on was a PDP-11/40, with a VT-05 terminal, I can say that the terminal drivers back then were pretty primitive.

    Also, having seen the famous photograph of Dennis Ritchie and Ken Thompson, working on the PDP where they developed Unix, I can tell you that they were working on an old LA-120, which was a teletype terminal.

    Now, I'm totally speculating, but given the nature of a teletype terminal, it's highly possible that the backspace COULD have easily been in the input stream -- at least as best as I can remember from my LA-120 experiences.....

    So, I would definitely say that the example in K&R is a bit dated.

    Man, that seems like the dark ages now!

  5. #5
    C Beginner()
    Join Date
    Oct 2004
    Posts
    22
    Thanks to all for your reply. I used getchar() to read a character from standard input (in my case, a console terminal.) I guess that what happens must be what Salem suggested. My terminal might not be passing the backspace character i input to the program i wrote. On the other hand, i find it a bit frustrating that i can't check this fact.

    By the way, filker0, which C textbook would you recommend for a newbie? I would like to get a book to read as i read K&R to fill in the "missing gaps" K&R might leave.

    Thanks to all.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    > On the other hand, i find it a bit frustrating that i can't check this fact.
    If you prepare a text file containing this character (and any other that you find hard to type at the keyboard), then you should be able to do.

    myprog.exe < testfile.txt

    Your program will see exactly those characters as it's input, without all that awkward character interpretation which is nice on an interactive prompt.

    Most "programmers editors" allow you to create files containing any characters you want. Things like notepad might balk at it though.

  7. #7
    C Beginner()
    Join Date
    Oct 2004
    Posts
    22
    Quote Originally Posted by Salem
    > On the other hand, i find it a bit frustrating that i can't check this fact.
    If you prepare a text file containing this character (and any other that you find hard to type at the keyboard), then you should be able to do.

    myprog.exe < testfile.txt

    Your program will see exactly those characters as it's input, without all that awkward character interpretation which is nice on an interactive prompt.

    Most "programmers editors" allow you to create files containing any characters you want. Things like notepad might balk at it though.
    Actually, i'm programming on linux (gentoo linux.) Anyway, i tried what you suggested but it does not work (at least on my machine.) I guess my terminal keeps getting in the way.

  8. #8
    C Beginner()
    Join Date
    Oct 2004
    Posts
    22
    Quote Originally Posted by Salem
    Who said anything about flushing stdin - something which you can't do anyway.

    All exercise 1-10 needs is getchar(), printf() and some if statements, all of which have just been described in the previous paragraphs.

    The line buffered nature of stdin means you're not likely to see a \b when typing in at the keyboard, since the terminal driver usually resolves that before passing the whole line to your program.
    I understand what you say. Is there an easy way (meaning, an easily understandable method for a beginner) to overcome the line buffered nature of stdin?

    I can add i have tried generating a plain text file containing the backspace character with the nano editor and feeding that file as input to my program with the command:

    ./kre1-10 < prueba.txt

    and \b does not show among the other characters.

    Well, that's all i can say right now. I hope you can help me on this subject.

    Thanks.

  9. #9
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235

    A little help

    Here is a short program that generates a file in the current working directory named "backspace.txt" that contains text with backspaces in it.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        FILE *fp = fopen("backspace.txt", "w");
        if (fp == NULL)
        {
            perror("unable to create file backspace.txt");
            return 1;
        }
        fprintf(fp, "This text contains b\ba\bc\bk\bs\bp\ba\bc\be\bs\n");
        fclose(fp);
        return 0;
    }
    Note that backspace is sometimes displayed as '^H' (control-h). In a hex dump, a backspace is a byte with a value of 0x08.
    Last edited by filker0; 01-09-2006 at 03:24 PM. Reason: fix omission in transcription
    Insert obnoxious but pithy remark here

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. intercepting packets for web servers and replying back
    By liri in forum Networking/Device Communication
    Replies: 1
    Last Post: 10-21-2007, 10:43 PM
  2. Intercepting I/O requests
    By Perspective in forum Linux Programming
    Replies: 2
    Last Post: 10-25-2006, 05:45 PM
  3. Intercepting information from another app
    By Pudnut in forum C# Programming
    Replies: 1
    Last Post: 09-02-2004, 09:42 AM
  4. Intercepting Window Messages?
    By amnesiasoft in forum Windows Programming
    Replies: 5
    Last Post: 10-27-2003, 12:45 PM
  5. ListBox items and intercepting messages
    By Mithoric in forum Windows Programming
    Replies: 2
    Last Post: 08-26-2003, 05:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21