Hi all,
I'm new at c. How do I make the system pause until any key is pressed in linux like getche does in borland?
thanks
getchar is the standard C library function. Borland compiler has many functions that have no standard C library equivalent, such as everything in graphics.h.
Well it's getchar but getchar is not exactly equivalent to the getche in borland c... By default, the standard input in c is in the canonical mode, which means that all input is line buffered and the line would be received only after the linefeed. This means that no matter what function we use for input, we have to press enter after anything we write, for it to become available. At least it must be so by standard, because standard input must be line buffered, if any compiler implements it otherwise for some standard c functions, then this compiler does not conform to standard. So, we cannot use input in non-canonical mode, in standard c, which means also that one cannot use compiler flags like -std=c89, to force a strict standard c, one must compile in that case without these flags.
So we cannot switch off the canonical mode by standard c functions, but we can do it using standard posix functions. POSIX is a standard which somewhat can be considered as an extension of the standard c library, as standard c library is a subset of it, but posix enables to do everything which is necessary to communicate with the operating system. In posix, there is a header termios.h, which provides functions necessary to change the terminal attributes, and the canonical mode is one of these. Unfortunately windows doesn't implement posix (though one can buy from ms some posix extension to windows or such), in spite that it's standard, so we cannot use the following in windows.
Using the graphical user interface like gtk, the input is always non-canonical, and happens character by character in the callback function which processes the key press event.
The code below is a "Hello World" program showing the non-canonical mode, the "Hellow World" appears immediately after pressing any key. There, the header unistd.h is necessary to define the file descriptions which termios functions use, 1 is the same as standard input. t and to there are structures with the type defined in termios.h, notice that we can assign one structure to another in standard c, the same way as we can do it with variables. Always restore the original terminal settings before exit, as, like, if you take echo off, you cannot see anything which you type on your terminal or console any more. Oh yes this gibberish, &= ~(|... Means that, we apply something to variable, using bitwise "and", and what we apply, is bitwise inversed, which means that when we apply bitwise or-ed set of flags that way, then that means that we set off these flags in the variable. May sound a bit complicated at first, but it's simple if you think a bit, and very important in programming, as this together with shift, is the common and fastest way to manipulate single bits.
Linux is a beautiful thing for programmers, where you never have to worry whether there is possible to do something or not, like it may happen in windows when you didn't buy some additional package or such. Have fun.
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
int main ()
{
struct termios to, t;
tcgetattr (1, &t);
to = t;
t.c_lflag &= ~(ICANON | ECHO);
tcsetattr (1, 0, &t);
if (getchar ())
printf ("Hello World!\n");
tcsetattr (1, 0, &to);
return 0;
}
>By default, the standard input in c is in the canonical mode, which means that all input is
>line buffered and the line would be received only after the linefeed.
That's not how buffering in C works. You're confused about the connection between a command shell and a C program. Canonical mode in a command shell is separate in that the shell's input buffer collects input and allows editing of that input before passing it on to the running program. Non-canonical mode sends characters directly to the running program as they're received by the shell. The running program doesn't know or care about the shell's buffering, it simply fills a stream with characters.
Line buffering is the default in C, but that buffer is different than the command shell's buffer in that it's used for performance reasons, not behavior reasons. You can switch I/O in C to be non-buffered and still be limited by the canonical mode of your shell.
What you mean with canonical mode is how the operating system handles I/O, which isn't covered by standard C, so system-dependent API calls need to be made to change the behavior of the I/O interface used by the standard C functions. This is made most obvious by the fact that your hello world program still uses the standard getchar function. You're not changing how standard C works, you're simply using non-standard functions to change how the system functions that standard C uses work.
>Oh yes this gibberish, &= ~(|... Means that, we apply something to variable, using bitwise
>"and", and what we apply, is bitwise inversed, which means that when we apply bitwise
>or-ed set of flags that way, then that means that we set off these flags in the variable.
That explanation is completely worthless to anyone except those of us who already know how it works. Perhaps instead of blowing a lot of hot air, you could give examples of how the bits are changed according to the truth tables of the bitwise operators. That kind of hard example is more likely to convey actual knowledge.
>Linux is a beautiful thing for programmers, where you never have to worry whether there
>is possible to do something or not, like it may happen in windows when you didn't buy
>some additional package or such. Have fun.
Maybe I'm missing something, but a summary of the OP's question is: "I know this can be done in Windows, but can it be done in Linux? If so, how?" I'm not sure if you're a Linux fanboy trying to make silly and irrelevant jabs at Windows, or if you're an arrogant Linux fanboy who looks down on everyone who doesn't have the same depth and breadth of knowledge.
At least you posted code.
> That's not how buffering in C works. You're confused about the connection between a command shell and a C program.
One should understand that it's not possible to implement standard input with line buffering without the canonical mode.
> That explanation is completely worthless to anyone except those of us who already know how it works. Perhaps instead of blowing a lot of hot air, you could give examples of how the bits are changed according to the truth tables of the bitwise operators.
Look, you even didn't say that my explanation is wrong, what it isn't, anyone who knows anything about logical operators can confirm that. This kind of thing can be said about any sentence said by anyone, because this is a subjective claim, deliberately left unsubstantiated, or in the other words simply a vituperation put into a decent looking form. And now i have a substantiated reason to be angry, so i'm justified to use all caps, sorry but some person here cause me to do that, i can obviously otherwise have no hope that he/she would understand, though there likely cannot be no such hope at all. NARUE, YOU OFFENDED ME BRUTALLY AND FOULLY <offensive material snipped>
And so, i was unfortunate enough to get offended, only because i posted here, trying to explain things and help newbies, with no bad intentions whatsoever towards anyone. WHAT MISTAKE DID I DO BY WRITING A POST TRYING TO HELP A PERSON, TO DESERVE *ANY* OFFENSES, EXPLAIN ME, OR YOU HIDE WHAT ARE YOUR *REAL* INTENTIONS, I HOPE THESE INTENTIONS ARE NOT TO TAKE POWER HERE BY VIOLENT MEANS, BUT AFTER THAT I OBVIOUSLY CANNOT BE SURE. I'm, btw, used to defend not only me, but *anyone* against violence, no matter in what form it appears. And this, btw, is not the first time when some person called Narue treated me badly here. And now, in the very day he/she came back, after a long time being absent, it was me who she chose to offend, this is another evidence of his/her sick intentions. So there is some reason why he/she doesn't like me, but i think these are some practical reasons that i in some way, unwantingly, endanger his/her position here or something. Everyone remember, this is most likely not about me personally, if he/she would succeed to get rid of me, most obviously *someone else*, whoever he happens to be, *would be next*. At least please, remember my words.
> I'm not sure if you're a Linux fanboy trying to make silly and irrelevant jabs at Windows, or if you're an arrogant Linux fanboy who looks down on everyone who doesn't have the same depth and breadth of knowledge.
This is another offense, but i, different from you, used to answer decently. In windows, you have to buy everything you may need, separately, there is no windows distribution which you can buy all at once. But in linux, you can install everything you may need, in no time at all, with a few push of the button. And there is most likely everything you may need, for example in more than 15 000 packages in debian. This is the difference which i talked about, and this is an objective fact, free of any intentions which you tried to ascribe to me.
> At least you posted code.
Yes i'm even able to write code, look, this is another offense as well.
>One should understand that it's not possible to implement
>standard input with line buffering without the canonical mode.
Incorrect. The C standard requires nothing of the sort. If you're going to spar with me on matters of the language standard, at least know what you're talking about.
>Look, you even didn't say that my explanation is wrong
Quite right, because your explanation was fine. However, it was also quite useless to someone who isn't familiar with the idiom because you failed to describe how it works.
>This is the difference which i talked about, and this is an
>objective fact, free of any intentions which you tried to ascribe to me.
Indeed? And what, pray tell does any of this have to do with producing the effect of getch on Linux? I don't have a problem with your opinions, but I do have a problem with your opinions diminishing the value of any legitimate technical help you attempt to provide.
Well, at least i don't listen to you any more.
OK, what i can say here, i'm forcefully violated against, i let it to everyone to think, what interests this may serve. My posts are edited against my will, a possibility to protect myself is taken from me here, if you even can read these words.
These of you who are interested, let's create our own place, only about c and linux. Because this place has even not so many visitors, and it's impossible to achieve peace here.
I don't think anyone will be interested in joining forces with a person who throws a tantrum when things don't go his way. Perhaps you should learn what I teach rather than prattling on and on about how you've been "violated". :icon_rolleyes:
I think you well understand that you can allow yourself anything now, you cut my tongue, and i may not even be able to reply. All the words are meaningless after that.
Your post was edited by an objective third party. In other words, you were at fault, and the verbal abuse was snipped from your attacks. If you keep it civil, you won't be censored. But if you want to be childish about it, I'll be happy to close this thread.
I think you well understand that you can allow yourself anything now, you cut my tongue, and i may not even be able to reply. All the words are meaningless after that.
That really isn't true -- you cut your own toung by posting abusive and malitious language about another member (did you bother to read the Rules ?). Otherwise you are pretty much free to post anything you want as long as it doesn't violate other DaniWeb rules.
Narue, don't you still understand that a peson, whose tongue is cut, no not only by you, but not this is important here, cannot reply you, so it doesn't make any sense to talk to me any more.
Well and you, Ancient Dragon, should understand the same as well. You in addition to everything blocked your private messages for me, in spite that i didn't write you anything offensive, except for asking to leave me alone. I always thought you are intelligent person with university education, so you must be able to understand.
Well and you, Ancient Dragon, should understand the same as well. You in addition to everything blocked your private messages for me
Huh? If you mean I have you on my ignore list ... I don't. I never block PMs -- ever. The initial PM you received was automatically generated by the system when I gave you the infraction. It works that way with everyone. And I think you get other autogenerated PMs when someone edits your posts.
except for asking to leave me alone
I only sent you courtesy PM in reply to the PMs you sent me.
You're beginning to annoy me with this "Woe is me! I broke the rules and got punished!" attitude. Get over yourself, you're no different from any other rule breaker.
Please at least don't start lying to me, the system don't let me send private messages to you. But if this is some system fault, then think how you treat people, from that depends how understanding are people towards you.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.