NO

Author Topic: Define - is stdin empty  (Read 13915 times)

vedro-compota

  • Guest
Define - is stdin empty
« on: October 20, 2011, 04:00:10 PM »
hi athlets)
Please tell me  - is there any way to define that the standart  input = stdin is empty ?

vedro-compota

  • Guest
Re: Define - is stdin empty
« Reply #1 on: October 20, 2011, 05:23:15 PM »
fgets() will wait input if it's clear so  I need to check

CommonTater

  • Guest
Re: Define - is stdin empty
« Reply #2 on: October 21, 2011, 06:29:20 PM »
Post your code... lets see what we're up against...

vedro-compota

  • Guest
Re: Define - is stdin empty
« Reply #3 on: October 21, 2011, 06:39:22 PM »
 :) it's the same code =
Code: [Select]
while (getchar() != '\n'); /* clear the input - stdin or use while (getchar() != '\n'); - but this'll stop program*/
       printf("\n%s",mtext );
       ch = getchar();
as i said  =
Quote
while (getchar() != '\n'); will stop program if the buffer is empty scanf() can get visible symbol from previous line which was put into input and hadn't been read at the whole - is there any way to check - is the buffer empty before call something which can make it so
so i need checking before input cleaning process (if fflush() isn't good variant)

CommonTater

  • Guest
Re: Define - is stdin empty
« Reply #4 on: October 21, 2011, 06:42:05 PM »
run the while trick AFTER reading your character with getchar...

When you enter a letter... you are actually putting two keystrokes in the buffer... the letter and the enter key.  Only the letter is taken out by ch = getchar();  the while loop cleans out any extraneous characters and the enter key ... *after* you get what you need.  In that case it won't block.

OR

use the scanf() trick I showed you...
Code: [Select]
scanf(" %c",&ch);

That will skip over crap in the buffer and wait for you to type something... note the space between " and %...

« Last Edit: October 21, 2011, 06:44:05 PM by CommonTater »

vedro-compota

  • Guest
Re: Define - is stdin empty
« Reply #5 on: October 21, 2011, 07:02:39 PM »
this's the second time you say) but problem is  wider . imagine that in the moment we call
Code: [Select]
mainmenu() (wich contain
Code: [Select]
printf("\n%s",mtext );)  we have in the stdin line ..mmmm...like this =
Quote
"dgfhj3  \n"
- it's  useless data - we need to remove it from stdin , so we can call while
Code: [Select]
(getchar() != '\n'); for examp. and remove it  - we'll have std in clear - and the first symbol (visible one , because the \n - will be removed during other mainmenu iteration - or other cleaning - it's not our problem =  we only need to correctly get the first symbol from the input line) will be read correctly ,

BUT

if we have  stdin in clear standing at that moment we call
Code: [Select]
mainmenu() - we'll have our program is stoped by technical (not related to user interface) reading - it isn't good - so there should be way to  avoid this stopping by checking the stdin stream (it may be empty).

how to check ? ;)
« Last Edit: October 22, 2011, 06:53:10 AM by vedro-compota »

CommonTater

  • Guest
Re: Define - is stdin empty
« Reply #6 on: October 21, 2011, 10:28:12 PM »
Ok... I don't know what the heck you're trying to do and all you ever post is 2 or 3 lines of code at a time, so it's really impossible for me (or anyone) to give you good advice... I've already steered you to tutorials and other documentation that you seem happy to ignore... 

Bottom line... you're just not giving us enough to help you.

FWIW... when a program starts, stdin is cleared... empty.  There's not going to be anything in there that you didn't put there with your keyboard.  There will never be a time when you need to worry about clearing the buffer *before* you make your first entry... the while trick clears the buffer after.  The extra space in scanf() discards invisible characters...  There are thousands of programs out there using these functions without any problems at all.

So, I'm just a little lost to know what your problem is...

vedro-compota

  • Guest
Re: Define - is stdin empty
« Reply #7 on: October 22, 2011, 07:02:40 AM »
Quote
when a program starts, stdin is cleared... empty
but what about situation in which this mainmenu function was called afret some time of program work?
for example =

Code: [Select]
int main()
{
   char ch;
                printf("\n%s","please specify a one symbol" ); /* but user've put "dgfhj3  \n"  */
                ch = getchar();
   /*in buffer we have "gfhj3  \n"*/
mainmenu();

return 0;
   
}
int mainmenu(void)
  {
     char ch;
char* mtext = " Please specify the number of the task. \n * You can choose on number from set = {1} \n * Specify \"0\" to exit\n" ;
char* errmes = " Error(!) = Main menu does not support this command.\n Make sure that your task number is from menu set of commands and try again. " ;

fflush(stdin); /* clear the input - stdin or use while (getchar() != '\n'); - but this'll stop program*/
       printf("\n%s",mtext );
       ch = getchar();

switch(ch)
     {
case '0': exit(0); 
case '1': exit(0); break;
default: printf("\n%s\n", errmes);
}
   return 0;
  }
« Last Edit: October 22, 2011, 08:08:09 AM by vedro-compota »

CommonTater

  • Guest
Re: Define - is stdin empty
« Reply #8 on: October 22, 2011, 08:24:43 AM »
You own the queue for your own program...

In Windows only the foreground window receives keystrokes ... thus there's never going to be a single keystroke in there that doesn't go in from your own keyboard *while your program is in the foreground* ... i.e. when it's sitting at your fgets(), scanf() or getchar() command itself. 

Quote
   /*in buffer we have "gfhj3  \n"*/
This won't happen.  Get it... no garbage in the queue... just the occasional trailing newline from pressing enter. 

You're making a huge fuss over something you don't even need to do...

Once again... your best bet is to climb into a proper C Programming textbook and do some serious study. 
Bluntly stated... You cannot "dumbass" your way through programming. 

Just write your menu in the more or less standard way...
Code: [Select]
int exit = 0;
int choice;

do
  {   
      printf("1) Do this\n");
      printf("2) Do that\n");
      printf("0) Exit\n");
     
      scanf (" %c",&choice);

      switch( choice )
        {
           case '1' :
              DoThisFunction();
              break;
           case '2' :
              DoThatFunction();
              break;
           case '0' :
              exit = 1;
              break;
            default :
               printf("Invalid choice");
         }
      }
    while(exit != 1);

Really... why make it more complex than it needs to be...
« Last Edit: October 22, 2011, 08:40:39 AM by CommonTater »

vedro-compota

  • Guest
Re: Define - is stdin empty
« Reply #9 on: October 22, 2011, 09:37:15 AM »
Quote
proper C Programming textbook
what does you imply?) I know your "c standard " and "The C Programming Language"  by Brian Kernighan and Dennis Ritchie)

CommonTater

  • Guest
Re: Define - is stdin empty
« Reply #10 on: October 22, 2011, 12:40:54 PM »
What I'm saying (not implying) in the gentlest way I can muster is that your questions are leading me to conclude that either you did not really understand the lessons very well or you are off on some odd tangent trying to do things in a manner that would surely never work to your advantage...  Or, forgetting my manners for a moment... that you "just don't get it".

C code is very simple code.  Programming itself, is hard.  But you seem to have a knack for making things even harder!  At first it was linux and windows in the same program, then it was GUI and console in the same program... now you're fussing over problems you'll never have... It's almost like you've grasped the syntax but not the process.

To make it even more confusing, you seem either unwilling or unable to give me (us) a real example of code you're working on so we can help you...  And, yes, if you would settle down to some good solid programming and ran into troubles, I would be more than happy to give you a hand when you get stuck...



Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: Define - is stdin empty
« Reply #11 on: October 22, 2011, 01:14:56 PM »
Quote
FWIW... when a program starts, stdin is cleared... empty.  There's not going to be anything in there that you didn't put there with your keyboard.
Except if you start program like this way from commandline.
Code: [Select]
echo 1|test.exetest it with this:
Code: [Select]
#include <stdio.h>

int main(int argc, char **argv)
{
char str[100];
fgets(str, sizeof(str)-1, stdin);
fputs(str, stdout);
return 0;
}
May the source be with you

CommonTater

  • Guest
Re: Define - is stdin empty
« Reply #12 on: October 22, 2011, 01:17:06 PM »
Quote
FWIW... when a program starts, stdin is cleared... empty.  There's not going to be anything in there that you didn't put there with your keyboard.
Except if you start program like this way from commandline.
Code: [Select]
echo 1|test.exetest it with this:
Code: [Select]
#include <stdio.h>

int main(int argc, char **argv)
{
char str[100];
fgets(str, sizeof(str)-1, stdin);
fputs(str, stdout);
return 0;
}

Or where you do .... myprogram < myfile.txt .... or such. 

In either case, the last thing you want to do is clear out the input buffer before reading anything.

 

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: Define - is stdin empty
« Reply #13 on: October 22, 2011, 01:38:52 PM »
I would not break input redirection by clearing the buffer first, since it will break established knowledge.



How about a simple testing program, which allows manual and redirected input, which is the case for all console programs, despite of being UNIX or Windows.
This will be used to check if buffer clearing is even needed, which I doubt at all.

A simple get input and display it unchanged will do the trick.
---
Stefan

Proud member of the UltraDefrag Development Team

vedro-compota

  • Guest
Re: Define - is stdin empty
« Reply #14 on: October 22, 2011, 03:28:54 PM »
concrect problem is here  = http://fkn.ktu10.com/?q=node/150 (it's just collection of moments which we've discussed with  specific wording of the problem)