•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 423,508 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,686 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 1254 | Replies: 17
![]() |
•
•
Join Date: Jul 2007
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
Hello!As a newbie programmer I don't know how to freeze my command line output,when I execute a C program,in order to see the output.Output opens and closes very fast.The trick I know from Pascal to put a 'Readln' statement as a last line doesn't work(with a 'scanf' or something like that.Could anybody give me a clue about that?Thanks in advance.
This was the most frequently asked question once in dev-c++ forum, and i'm the most sure this topic has already been discussed many times in this forum as well. Maybe the shortest answer, use only fgets for input, this is the only right function for input, as it restricts the number of input characters, and it would always input the newline as well. Then you can easily use getchar to prevent program from ending. But if you still want to use some weird input methods, like cin, then you have to put fflush (stdin); before getchar.
A common way of pausing the program is to make use of the standard function getchar().
scanf() behaviour will mess up your ability to pause the program with getchar() sometimes. Read about it here.
scanf() behaviour will mess up your ability to pause the program with getchar() sometimes. Read about it here.
Last edited by Aia : Jul 15th, 2007 at 6:33 pm.
"No man's life, liberty, or property is safe while the legislature is in session." ~ Mark Twain
•
•
•
•
But if you still want to use some weird input methods, like cin, then you have to put fflush (stdin); before getchar.
Best way is to avoid any `weird input methods' and do not start even
using fflush( stdin ) at all. Here is why not to use it.
"No man's life, liberty, or property is safe while the legislature is in session." ~ Mark Twain
Yes that's right, effect of fflush is undefined for stdin and it of course must not be used. But if they already do things insecurely, then they must use other uncertain things to compensate it, nothing to do, their choice. Then i say again that these who read would better remember, use only fgets for input. And if you use anything else for input, then delete all rubbish and use only fgets for input.
>Yes that's right, effect of fflush is undefined for stdin and it of course must not be used.
Then why did you tell the OP to use it?
>But if they already do things insecurely, then they must use other uncertain things to compensate it
There are better ways to discard pending characters than to introduce undefined behavior. The easiest is a simple loop:
If you can't suggest one of the better options, you're better off sticking to the original advice of always using fgets.
Then why did you tell the OP to use it?
>But if they already do things insecurely, then they must use other uncertain things to compensate it
There are better ways to discard pending characters than to introduce undefined behavior. The easiest is a simple loop:
void jsw_flush ( void )
{
int ch;
do
ch = getchar();
while ( ch != EOF && ch != '\n' );
clearerr ( stdin );
} I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Thanks Narue, discarding pending characters is indeed better than fflush (stdin). I never use fflush (stdin), and thus tend to forget what standard says about it. This discarding pending characters until newline or eof is good when using fgets as well, because fgets doesn't do that. fgets isn't perfect input method, but the only way not to do it character by character. And for not doing anything character by character, the only option is to have the number of input characters large enough, so that the user couldn't likely write too many characters by mistake. I usually use FILENAME_MAX as size, this is 256 or so, and a defined constant, so not a magic number. But, when the user still manages to write too many characters before newline, then the next input would consume it, and would most likely cause a wrong input. The standard doesn't guarantee either, that the input stream would be flushed when the program exits, though mostly it will be flushed.
Last edited by TkTkorrovi : Jul 15th, 2007 at 8:46 pm. Reason: none
>I usually use FILENAME_MAX as size
I prefer BUFSIZ, personally.
>then the next input would consume it, and would most likely cause a wrong input.
That's a quality of implementation issue. You write your code so that it can intelligently handle excessively long lines.
>The standard doesn't guarantee either, that the input
>stream would be flushed when the program exits
I'm not sure where you're going this.
I prefer BUFSIZ, personally.
>then the next input would consume it, and would most likely cause a wrong input.
That's a quality of implementation issue. You write your code so that it can intelligently handle excessively long lines.
>The standard doesn't guarantee either, that the input
>stream would be flushed when the program exits
I'm not sure where you're going this.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
> I prefer BUFSIZ, personally.
Yep, matter of taste, just because i must use FILENAME_MAX for file names, i prefer to use the same constant everywhere.
> That's a quality of implementation issue. You write your code so that it can intelligently handle excessively long lines.
Yes and this can only be done character by character, at least you have to consume pending characters that way. I talked about input which is not done character by character, using only fgets, which is not exactly a quality implementation, but just a somewhat satisfactory option.
> I'm not sure where you're going this.
??
Yep, matter of taste, just because i must use FILENAME_MAX for file names, i prefer to use the same constant everywhere.
> That's a quality of implementation issue. You write your code so that it can intelligently handle excessively long lines.
Yes and this can only be done character by character, at least you have to consume pending characters that way. I talked about input which is not done character by character, using only fgets, which is not exactly a quality implementation, but just a somewhat satisfactory option.
> I'm not sure where you're going this.
??
Knowledge is regarded by the fool as ignorance, and the things that are profitable are to him hurtful. He liveth in death. -- Thoth the Atlantean
I also use FILENAME_MAX because it used to be somewhat smaller that BUFSIZ, in Linux, BUFSIZ used to be some 8000, and FILENAME_MAX some 4000, it's smaller, but quite enough so that it is quite difficult to type in so many characters. FILENAME_MAX is somewhat related to the size of the command line, as it has to allow a long file name which still has to be shorter than the maximum command line, while BUFSIZ is not exactly for such purpose, it's likely bigger in the systems with more resources.
Knowledge is regarded by the fool as ignorance, and the things that are profitable are to him hurtful. He liveth in death. -- Thoth the Atlantean
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Installing Windows 98 On VMware. Floppy problem (Windows 9x / Me)
- Newbie - looping and array [very simple] problem (PHP)
- Help me with this Simple Problem plss (C)
- BUTTON DOES NOT WORK??? simple problem (C)
- Simple problem regardingform elements (ASP)
- Mac Mouse Problem! Help me out? (OS X)
Other Threads in the C Forum
- Previous Thread: Convert MarkDown to MarkUP
- Next Thread: C with XML



Linear Mode