| | |
A simple problem
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2007
Posts: 5
Reputation:
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 7:33 pm.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
•
•
•
•
But if you still want to use some weird input methods, like cin, then you have to put fflush (stdin); before getchar.
using fflush( stdin ) at all. Here is why not to use it.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
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:
C Syntax (Toggle Plain Text)
void jsw_flush ( void ) { int ch; do ch = getchar(); while ( ch != EOF && ch != '\n' ); clearerr ( stdin ); }
New members chased away this month: 4
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 9: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.
New members chased away this month: 4
> 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
![]() |
Similar Threads
- Installing Windows 98 On VMware. Floppy problem (Windows 95 / 98 / 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
Views: 1955 | Replies: 17
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char character convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory drawing dynamic executable feet fflush fgets file floatingpointvalidation fork frequency function getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide infiniteloop initialization interest intmain() kilometer lazy license linked linkedlist linux linuxsegmentationfault list match matrix meter microsoft multi mysql odf open openwebfoundation pattern pause pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition scheduling segmentationfault send shape single socketprogramming spoonfeeding stack standard strchr string strings structures student suggestions system test testautomation unix urboc user voidmain() win32 win32api windows.h






