Pelles C forum

C language => Beginner questions => Topic started by: vedro-compota on October 20, 2011, 04:00:10 PM

Title: Define - is stdin empty
Post by: vedro-compota 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 ?
Title: Re: Define - is stdin empty
Post by: vedro-compota on October 20, 2011, 05:23:15 PM
fgets() will wait input if it's clear so  I need to check
Title: Re: Define - is stdin empty
Post by: CommonTater on October 21, 2011, 06:29:20 PM
Post your code... lets see what we're up against...
Title: Re: Define - is stdin empty
Post by: vedro-compota on October 21, 2011, 06:39:22 PM
 :) it's the same code = 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  =
Quotewhile (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)
Title: Re: Define - is stdin empty
Post by: CommonTater 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...

scanf(" %c",&ch);


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

Title: Re: Define - is stdin empty
Post by: vedro-compota 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 mainmenu() (wich contain 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 (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 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 ? ;)
Title: Re: Define - is stdin empty
Post by: CommonTater 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...
Title: Re: Define - is stdin empty
Post by: vedro-compota on October 22, 2011, 07:02:40 AM
Quotewhen 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 =

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;
  }
Title: Re: Define - is stdin empty
Post by: CommonTater 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...

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...
Title: Re: Define - is stdin empty
Post by: vedro-compota on October 22, 2011, 09:37:15 AM
Quoteproper C Programming textbook
what does you imply?) I know your "c standard " and "The C Programming Language"  by Brian Kernighan and Dennis Ritchie)
Title: Re: Define - is stdin empty
Post by: CommonTater 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...


Title: Re: Define - is stdin empty
Post by: TimoVJL on October 22, 2011, 01:14:56 PM
QuoteFWIW... 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.echo 1|test.exetest it with this:#include <stdio.h>

int main(int argc, char **argv)
{
char str[100];
fgets(str, sizeof(str)-1, stdin);
fputs(str, stdout);
return 0;
}
Title: Re: Define - is stdin empty
Post by: CommonTater on October 22, 2011, 01:17:06 PM
Quote from: timovjl on October 22, 2011, 01:14:56 PM
QuoteFWIW... 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.echo 1|test.exetest it with this:#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.

Title: Re: Define - is stdin empty
Post by: Stefan Pendl 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.
Title: Re: Define - is stdin empty
Post by: vedro-compota on October 22, 2011, 03:28:54 PM
concrect problem is here  = http://fkn.ktu10.com/?q=node/150 (http://fkn.ktu10.com/?q=node/150) (it's just collection of moments which we've discussed with  specific wording of the problem)
Title: Re: Define - is stdin empty
Post by: vedro-compota on October 22, 2011, 03:41:28 PM
last attempt - concrect problem is here  = http://fkn.ktu10.com/?q=node/150 (http://fkn.ktu10.com/?q=node/150) (it's just collection of moments which we've discussed with  specific wording of the problem)

CommonTater , I totally agree with your advices about way of programming stading and i also "just get it" about scanf() and other - big thanks for this - especially for s standard pdf))
i'm really try to understand problems which may be not important  in current situation  - and mau be it isn't right or isn't good.

( scanf(" %c", ch) - will shut program down after few seconds of working  - compiler 'll show warning that the "ch" variable isn't in use - I don't understand  why it's so - but that's "other thread" as I understand )

i'm just want to understand as much as possible about with which i deal) The c standart will really help me with this = you've really helped me with this - big thanks)
Title: Re: Define - is stdin empty
Post by: CommonTater on October 22, 2011, 06:30:09 PM

int mainmenu(void)
  {   
     int 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. " ;       

     printf("\n%s",mtext );     
     ch = getchar() ;
     while (getchar() != '\n');

     switch(ch)   
        {
           case '0':   
               printf("\nyou choise is item 0 \n" );
               break;
           case '1':
               printf("\nyou choise is item 1 \n" );
               break;       
           default:
              printf("\nInvalid choice\n");   
         }   
      return 0; 
}


Quote
scanf(" %c", ch) - will shut program down after few seconds of working  - compiler 'll show warning that the "ch" variable isn't in use - I don't understand  why it's so - but that's "other thread" as I understand

scanf (" %c", &ch);


Title: Re: Define - is stdin empty
Post by: vedro-compota on October 24, 2011, 04:14:54 PM
thank you)