Thread: Confused Reading Command Line Argements

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    80

    Confused Reading Command Line Argements

    I'm using Windows XP and Visual Studio 200.

    I have a command line program named test1. There are three arguments that are passed to it:
    1. Name of the program
    2. Input 1 (SMILES String)
    3. Input 2 (fragment)

    I figured out how to get/print the first letter from each argument:
    Code:
    #include <stdio.h>
    #include "stdafx.h"
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int i;
    	int correctParams = 1;
    
    	if ( argc != 3 ) {
    		printf( "Enter a Smiles string and fragment your searching for\n" );     
    		correctParams = 0;
    	}
    	if (correctParams == 1) {
    
    		for (i = 0; i < argc; i++)
    			if (i == 2) {
    			printf("argv[%d] = \"%s\"\n", i, argv[i]);
    			}
    
    	}
    
    	return 0;
    }
    I'm wandering how you get all of the letters (argv[something?]. Basically, I would like to set the 3 different arguments to 3 different variables

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Code:
    int length;
    char *arg1;
    
    length=strlen(argv[1]);
    if(arg1=malloc(length+1);)
        strcpy(arg1,argv[1]);
    else
        //Error code
    //Other code
    free(arg1);
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    80
    Thanks King Mir.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    80

    Program not liking strlen

    For some reason, the program errors on strlen, its saying identifier not found:

    Here's what I've changed my code to:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include "stdafx.h"
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int i;
    	int correctParams = 1;
    	int length;
    	char *arg1;
    
    	if ( argc != 3 ) {
    		printf( "Enter a Smiles string and fragment your searching for\n" );     
    		correctParams = 0;
    	}
    	if (correctParams == 1) {
    
    		length=strlen(argv[1]);
    
    	}
    
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems in reading binary file
    By serena in forum C Programming
    Replies: 3
    Last Post: 04-14-2005, 03:54 AM
  2. Reading a record from a File
    By David101 in forum C Programming
    Replies: 2
    Last Post: 12-14-2004, 06:42 PM
  3. question about reading in strings from a file :>
    By bball887 in forum C Programming
    Replies: 8
    Last Post: 04-13-2004, 06:24 PM
  4. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM
  5. behind and confused
    By steviecrawf in forum C Programming
    Replies: 1
    Last Post: 11-09-2001, 12:51 PM