Get input using tab instead of Enter key in C-Linux - c

Background:
I am developing linux-like shell in C. The first and basic requirement is to get infinite input (probably commands like rm kill etc. , which will execute) from user & upon Tab key, list of possible commands must be output to the screen.
Question:
My question is that how to get the input upon Tab Key press instead of Enter key?
A quick example will be very helpful, since I'm a college student learning C as part of OS course.

You want receive already entered chars for autocomplete?
I suggest you to get each key press separately.
Thus, it will always be known that user have already entered.
#include <stdio.h>
getchar()
or
#include <conio.h>
_getch()
https://en.wikibooks.org/wiki/A_Little_C_Primer/C_Console_IO
Tab is '\t' and Enter is '\n'
while ((c = getchar()) != EOF)
{
if (c == '\n')
++newlines;
else if (c == '\t')
++tabs;
else if (c == ' ')
++blanks;
if (c == EOL) {
printf("Lines: %d\nTabs: %d\nBlanks: %d\n", newlines, tabs, blanks);
}
}

Related

Reading delete keystroke in c

I have encountered something I don't have a clear grasp on. I am having trouble reading the delete input from my MacBook Air. I am writing a program in c to read the input, and have used the following escape sequence character:
c = '127'
I can set the variable c to the decimal value of 8, which from my understanding is the backspace escape sequence. And also in Terminal>Preferences>Advanced, I check the box that enables delete to send Control-H.
Once I do the mentioned things above, I can read the input setting c variable as the following:
c = '8'
Here is my code to attempt to read the delete key from my MacBook
Air:
#include <stdio.h>
main() {
/* Copy program input to program output,
replacing each tab by \t,
each backspace by \b,
and each backslas by \\.
This makes tabs and backspaces visible in an unambiguous way.
*/
int c; // a variable for a character
while((c = getchar()) != EOF) {
if (c == '\t')
printf("\\t");
// check box found in:
// Terminal>Preferences>Advanced>Delete sends Control-H
// default setting DOES NOT send Control-H
// The Delete key does not appear to work with the ascii decimal character value 127.
if (c == '\127')
printf("Print to the screen if Delete key is detected.");
if (c == '\\')
printf("\\\\");
if (c != '\t')
if (c != '\b')
if (c != '\\')
putchar(c);
}
}
Try the following code and see what happens:
#include <stdio.h>
#include <stdlib.h>
int
main() {
int c;
system("stty raw -echo");
while (1) {
c = getchar();
if (c == 3) break; // exits when Ctrl-C is pressed
printf("pressed code: %d\r\n", c);
}
system("stty -raw echo");
return 0;
}
Hope this helps.
The reason that you can't read it is that it is not actually there. Using the delete code to delete characters is inefficient, so most programs actually delete the character instead of adding a delete character in order to hide the previous character.

count tabs in C (fails)

So, I'm supposed to be creating a program in C to count: "blanks, tabs, and spaces". My code is provided below (including comments). you'll see that I have commented the areas of the code that is tasked with counting, and displaying the count of, tabs.
As it stands, the program counts "Blanks" and "Lines" accurately, but "Tabs" always results in "0". I've combed over the tiny code again and again looking for typoes or something, but have found no reason why '\t' fails to be counted. Then I came here and read through a few posts dealing with this same challenge. Thus far, I have found nothing anywhere which would shed any light on the problem I am experiencing with my code.
please, look it over, compile it, and test it out yourself to verify.
If anyone can tell me where my mistake is, that is causing my code not to count "Tabs" (as desired), that would be fantastic.
thank you.
#include <stdio.h>
/* count blanks (" "), tabs (\t), and newlines (\n) */
main()
{
int c, nb, nt, nl;
nb = nt = nl = 0;
while ((c = getchar()) != EOF)
if (c == ' ')
++nb;
else if (c == '\t')
++nt; /* fails to accumulate count */
else if (c == '\n')
++nl;
printf("Spaces:\t%d\n", nb);
printf("Tabs:\t%d\n", nt); /* fails to report new tabs (\t) */
printf("Lines:\t%d\n", nl);
}

How come \b is not recognized from ex. 1-10 in K&R?

#include <stdio.h>
/* replace tabs and backspaces with visible characters */
main()
{
int c;
while ((c = getchar()) != EOF) {
if (c == '\t')
printf("\\t");
if (c == '\b')
printf("\\b");
if (c == '\\')
printf("\\\\");
if (c != '\b')
if (c != '\t')
if (c != '\\')
putchar(c);
}
}
Why was I not able to see \b backspace signature when I press the backspace?
You need to learn about else, that if-ladder is pretty scary.
And your terminal probably doesn't send a single backspace character, it can be a bit complicated how actual terminal programs represent that kind of "special" key (delete is another favorite).
If you're on a unix-like system, you probably want to read this: http://en.wikipedia.org/wiki/Cooked_mode
On other operating systems, I don't know, but they are also likely to do things to your input.
Some of the characters handled by Terminal. So you can't get control over it. Check this answer.
I tried in my MAC terminal. But i didn't get the value 127 or 8 like, in this answer. I got 32 for backspace character. So when i tried the if condition with 32, it printed out the \b value.
if (c == 32 || c == 8)
printf("\\b");

Program not printing out numbers

I'm a beginner in C and I bought the book "The C programming language" and started reading and doing everything it says. There is a code in the book which should print out the number of lines,words and characters of a sentence. This is the code of the book.
#include <stdio.h>
#define IN 1 /* inside a word */
#define OUT 0 /* outside a word */
/* count lines, words, and characters in input */
main()
{
int c, nl, nw, nc, state;
state = OUT;
nl = nw = nc = 0;
while ((c = getchar()) != EOF) {
++nc;
if (c == '\n')
++nl;
if (c == ' ' || c == '\n' || c = '\t')
state = OUT;
else if (state == OUT) {
state = IN;
++nw;
}
}
printf("%d %d %d\n", nl, nw, nc);
}
For some reason the values that should be printed out with the printf aren't shown. I really don't know what is wrong. It only prints out numbers if I put the printf in the while loop but this can't be right because it prints the numbers every time the values change.
You have a typo in this line -- the assignment of c = '\t' always evaluates to true
if (c == ' ' || c == '\n' || c = '\t')
To fix, change = to ==
if (c == ' ' || c == '\n' || c == '\t')
To answer your question why does it not print anything out -- I think that the compiler is either giving your an error or warning causing the program not being linked, and hence you are not actually compiling the source code as listed, but rather you are running an old version of whatever code you compiled before.
gcc gives a compile error on this line:
if (c == ' ' || c == '\n' || c = '\t')
looking # that line, it's easy to spot a typo: c = '\t')
missing an equality test, no?
:)
It looks like you are reading your input from STDIN (the keyboard) and leave the loop only on "End of file", which will not happen. Do as the other answer suggests.
Besides the typo others have mentioned, you need to send an End-Of-File signal to end the input loop. This will depend on the environment you are running it - will be Ctrl+Z in Windows, and should be Ctrl+D in *NIX environments. I needed to enter this on a new line and press Enter afterwards when using my usual IDE (Code::Blocks).
Remember too that at the time K&R (1 & 2) was written, command line programming was the norm, and typically you would expect the output to be visible if you just ended the program. Some IDEs will close their terminal emulator at the end of the program before you can view the results, so you may need to add something like
printf("\nPress return to continue");
getchar();
at the end of the program. Or, run it yourself from the OS terminal emulator (using cmd.exe on Windows, or gnome-terminal, yakuake, or whatever your OS provides). Navigate to the folder and
your_executable.exe
or
./your_executable
it was a equality issue as pointed above in if (c == ' ' || c == '\n' || c = '\t').
I would suggest you use a good IDE like Eclipse CDT which warns you of such bugs .

How can i make that the console, with the output, will not disapear after the program ends in VS 2010 express C++

How can i make that the console, with the output, will not disapear after the program ends in VS 2010 express C++?
i write in c and not in c++, soo i need a function and include path to library.
Thanks
You can simply poll for input. This performs a block so that the function only returns when the user gives more input - usually enter. If you're on Windows you can also use system("PAUSE").
You have a few options:
Run the program from command prompt
Add a getchar() before you return from main.
Add system("pause") before you return from main
Pressing Ctrl+F5 ("Build -> Start Without Debugging") will run the application and automatically wait for a keypress before closing the console. However, as the name says, you do not have a debugger attached then.
int waitforenter(void) {
int ch;
puts("press ENTER (maybe twice)");
/* get rid of a (possibly) pre existing '\n' */
do {
ch = getchar();
} while ((ch != EOF) && (ch != '\n'));
/* and again */
if (ch != EOF) do ch = getchar(); while ((ch != EOF) && (ch != '\n'));
return ch;
}
And then call waitforenter() right before the end of your main() function.

Resources