You are not logged in.
Hi there,
I started to learn C.
I'm reading K&R 2nd edition (ANSI C) in my native language.
I've problems with a basic exercise on getchar and putchar, chapter 1:
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 tabs and backspaces visible in an unambiguous way.
So I did:
#include <stdio.h>
/* Exercise 1.10
*/
int main(){
int c;
while ((c=getchar()) != EOF){
if (c=='\t'){
putchar('\\');
putchar('t');
}
if (c=='\b'){
putchar('\\');
putchar('b');
}
if (c=='\\'){
putchar('\\');
putchar('\\');
}
else putchar(c);
}
return 0;
}
ok for tabs
not good for backslash: I can omit the if statement, the backslash is reported correctly in output. Why?
not good for backspace: simply the output is the same as input, without deleted chars and without \b. Why?
Did I wrong?
Offline
Have you already learned how to use a debugger? If not, this is the time to do it. This will help you more than if someone tells you a solution.
Offline
For me both the tab and the backslash display correctly.
About the backspace, I think the problem is the input buffer is processed before it gets to the input of the program and the backspace is removed
Offline
Have you already learned how to use a debugger? If not, this is the time to do it. This will help you more than if someone tells you a solution.
no, I'm using geany for write my own programs but I only use F9 (compiling) and F5 (executing) shortcuts.
I suppose that geany will also deliver a debugger to the user. I'll give a look.
For me both the tab and the backslash display correctly.
About the backspace, I think the problem is the input buffer is processed before it gets to the input of the program and the backspace is removed
Do you tried the program on your own computer?
I googled a bit and the input buffer could be a clue. But I don't know how a buffer works
Offline
Yep tried it on my computer.
AFAIK you need to use something like ioctl() (see the man page for more info)
Offline
For me both the tab and the backslash display correctly.
About the backspace, I think the problem is the input buffer is processed before it gets to the input of the program and the backspace is removed
Actually, I did not chime in because the OP said the backslash did not display. You are saying it works.
In that case, the problem with the backspace is here:
}
if (c=='\\'){
putchar('\\');
putchar('\\');
}
else putchar(c);
This last clause is not protected by an else coming out of the code that handles the backspace (The backspace code ends at the } on the first line I quoted.)
Next, either the putchars that print \\ will execute, or the putchar{c} will execute. If we are processing backspace, then we should have already printed \b, As the value of c is not \, we skip the code that prints the \\ and instead print the value of c. Since it is a backspace, it backs over the 'b' you printed as part of the \b.
Try putting else statements before the second and third 'if's and see what happens.
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way
Offline
To get unbuffered input, take a look at tcgetattr() and tcsetattr() using man or google.
Another hint: Your terminal may not input '\b' for the backspace key - mine did not. Check its settings. Debugging will help here as well.
As was previously said by ewaller, your current code will put a backspace after "\b" and erase the b again. Since the exercise states "replace", the if code should look like this:
if (...) {
...
} else if (...) {
...
} else if (...) {
...
} else {
...
}
Now, what is a buffer: A buffer is a temporary memory where, in this case, the input will be stored in, before your application is notified. This is very useful: In many applications, it is not necessary to care for the raw, unbuffered input but instead for the final input of the user (using backspace to make corrections). The buffered input is sent once the user inserts a newline - at least this is the only implementation I know of.
Offline