Handling scanf(reading values) in a program at once while executing the program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chansra agnuru
    New Member
    • Apr 2011
    • 6

    Handling scanf(reading values) in a program at once while executing the program

    Hi,

    I need some information regarding how to handle scanf values while executing the program itself.
    For example:
    Code:
    main(int argc, char* argv[])
    {
    
    char str[20];
    printf("first argument: %s",__argv[1]);
    printf("Second argument: %s",__argv[2]);
    
    printf("Enter your name:");
    gets(str);
    
    )
    suppose the program exe name is BYTE then in order to execute from command prompt we need to give:
    BYTE 1arg 2arg ->ENTER
    it asks to enter your name

    My question is how can i give name at executing itself
    like
    BYTE 1arg 2arg << anna hazare

    will it work???
    Last edited by Meetee; Apr 13 '11, 05:35 AM. Reason: Code tags added
  • Peter Lesslie
    New Member
    • Apr 2011
    • 5

    #2
    You could rewrite the program to check if argc had 3 arguments. If it did then use that as the name, if argc == 2 then ask for the name.

    Comment

    • chansra agnuru
      New Member
      • Apr 2011
      • 6

      #3
      ya the program required two arguments like i mentioned, but how can i get rid of entering name after execution??..is their any facility to read vlues for scanf at execution time itself??

      Comment

      • Peter Lesslie
        New Member
        • Apr 2011
        • 5

        #4
        This is what I mean:

        Code:
        #include <stdio.h>
        
        int main(int argc, char* argv[])
        {
        if ( argc == 3 ) {
        printf ("first argument: %s",__argv[1]);
        printf ("second argument: %s",__argv[2]);
        printf ("Name: %s",__argv[3]);
        }
        else {
        char str[20];
        printf("first argument: %s",__argv[1]);
        printf("Second argument: %s",__argv[2]);
        
        printf("Enter your name:");
        gets(str);
        }
        
        return 0;
        
        }
        Last edited by Meetee; Apr 13 '11, 05:36 AM. Reason: Please add code tags while posting code

        Comment

        • chansra agnuru
          New Member
          • Apr 2011
          • 6

          #5
          I agree with you in the first case (__argc==3) i am asking about else part which you have sent.From the command prompt we can execute a program by giving required arguments right.so how can i give values for a program during execution time(like arguments)inste ad giving at run time and press ENTER for scanf??.I hope this will clear :-)

          Comment

          • Peter Lesslie
            New Member
            • Apr 2011
            • 5

            #6
            On a *nix system you could use something like
            Code:
            echo "What ever you want to pass" | a.out
            where a.out is your program. Is that what you mean? (I'm not sure how/(if it exists) the pipe works on Windows.

            Comment

            • chansra agnuru
              New Member
              • Apr 2011
              • 6

              #7
              Yes by using the above command we can pass value to only one scanf/gets. If the program is having multiple scanfs then how can we pass?For better understanding am sending the code..
              Code:
              int main( int argc, char* argv[])
              {
              char buf[20];
              char bug[20];
              printf("fisrt arg:%s\n",argv[1]);
              printf("second arg:%s\n",argv[2]);
              
              printf(" enter name1:");
              gets(buf);
              
              printf("Enter name2:");
              gets(bug);
              
              printf("\n First Scanf:\n");
              puts(buf);
              printf("second scanf: \n");
              puts(bug);
              printf("\n********* END*****");
              return 0;
              }
              If I give: echo "Peter Lesslie" | ./a.out FIRST SECOND
              The Output is: echo "Peter Lesslie great" | ./a.out FIRST SECOND
              fisrt arg:FIRST
              second arg:SECOND

              enter name1:
              Enter name2:
              First Scanf:
              Peter Lesslie great
              second scanf:

              But i want out put like:
              First Scanf:
              Peter
              second scanf:
              Lesslie great

              Is this any seperator required in echo???
              Last edited by Meetee; Apr 13 '11, 05:36 AM. Reason: Code tags added

              Comment

              • Peter Lesslie
                New Member
                • Apr 2011
                • 5

                #8
                Code:
                #include <cstdio>
                
                int main( int argc, char* argv[])
                {
                char buf[20];
                char bug[20];
                printf("fisrt arg:%s\n",argv[1]);
                printf("second arg:%s\n",argv[2]);
                 
                printf(" enter name1:");
                scanf("%s",buf);
                 
                printf("Enter name2:");
                gets(bug);
                 
                printf("\n First Scanf:\n");
                puts(buf);
                printf("second scanf: \n");
                puts(bug);
                printf("\n********* END*****\n");
                return 0;
                }
                This works for me. (gets is not delimited by spaces so it will take all the initial input while if you make the first one a scanf then it won't do that.

                Comment

                • chansra agnuru
                  New Member
                  • Apr 2011
                  • 6

                  #9
                  Hi,

                  I have tried
                  echo "peter\nLes slie great"| ./a.out FIRST SECOND

                  It worked.Thanks for the information you have given till now though i have squeezed you!!.. :-))

                  Comment

                  Working...