Thread: Scaning chars while initializing program?

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    23

    Scaning chars while initializing program?

    Hello. I was wondering if anyone knew how to scan in a string if you place it right after how to initiate the program.
    i.e. a.out ContentsOfString

    and also if anyone could help out... i need to scan the line below it into a string also.
    so...

    a.out ContentsOfString
    String2contets

    any advice could help.

    Thanks, Curtis

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    37
    Is your requirement parsing the command line arguments?

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    57

    Re: Scaning chars while initializing program?

    Tell me your requirement or problem clearly.
    whether you want to parse a command line argument or read the string before initialize the program(Like configuration )
    or do you want to read a line

    If you want to get a arguments you can use argv in the main function
    int main(int argc,char **argv)
    Last edited by sganesh; 03-02-2010 at 03:24 AM.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    23
    i need to be able to save both the strings to use them in a cypher. But i also must run the program as
    $ a.out key <---The key used for the cypher

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    The following program is reading the keys passed to the program and print them.For example,I simply printed the arguments.If you have any argument,you use those strings in your cypher.

    Code:
    #include	<stdio.h>
    	int
    main ( int argc, char *argv[] )
    {
    	int i=0;
    	if(argc==1)
    		printf ( "Please Enter the Command line arguments\n"  );
    	else
    	{
    		for(i=1;i<argc;i++)
    			printf ( "Arguments Passed Are:%s\n", argv[i] );
    	}
    	return 0;
    }

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Creedy, the C program can be passed, at start up, many parameters - that's just no problem at all.

    They are loaded into a char array with multiple rows, with one row for each word.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    66
    I guess you wanna give a string with multiple lines in a command line argument and parsing it ?
    If yes
    give the string in a command line enclosed in ""
    $ a.out string1 "string2
    continuation of string 2
    ....... etc "

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    23
    Thank you vivekraj, Adak, and Alexander jack. I understand it better now, it all just came together. I think i should be able to finish the program now. Thanks.

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    23
    Dangit. One more issue. If i want the first letter of the key then i would printf argv[ i ][ 0 ] ?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Feb 2010
    Posts
    23
    when i try to print it i get an error:
    format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’

  12. #12
    Registered User
    Join Date
    Feb 2010
    Posts
    37
    While printing the first character alone use %c instead of %s

  13. #13
    Registered User
    Join Date
    Apr 2009
    Posts
    66
    Code:
                            printf ( "First char :%c\n", argv[i][0] );

  14. #14
    Registered User
    Join Date
    Feb 2010
    Posts
    23
    Okay cool. Thanks everyone.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By the way, a command line argument might be a zero length string, which means that argv[i][0] for that command line argument would be a null character. I am not entirely sure how that factors in when you are trying to print it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A lottery program - arrays, ints, chars etc
    By Glauber in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2008, 10:48 AM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM