command line parameters

This is a discussion on command line parameters within the C Programming forums, part of the General Programming Boards category; Forgive my question if it's easily answered, but last night I don't think I was quit getting it / figuring ...

  1. #1
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    command line parameters

    Forgive my question if it's easily answered, but last night I don't think I was quit getting it / figuring it out. Would there be a "good" way to pass 4 command line parameters to a program, then house this in a function so you can change them at will?

    Here's an extremely hackish example as I couldn't figure much else out:
    Code:
    int ExtractZip( char * SourceFile, char * Destination, char * Name )
    {
    	FILE * Tmp;
    	if( ( Tmp = fopen("Tmp.bat", "a") ) == 0 )
    	{
    		/* Nothing */
    	}
    	else
    	{
    		fprintf(Tmp, "@echo off\n");
    		fprintf(Tmp, "d:\\unzip.exe ");
    		fprintf(Tmp, "%s", SourceFile);
    		fprintf(Tmp, " -d ");
    		fprintf(Tmp, "%s >NUL", Destination);
    		printf("Copied:  %s\n", Name);
    	}
    	fclose(Tmp);
    	system("tmp.bat");
    	remove("tmp.bat");
    	return 0;
    }
    System will obviously not work for this, if it did it would be fine as the intended platform has system, but, system can't accept changable arguments.

    I looked into spawn, but couldn't figure much out.
    Typical command line:
    unzip.exe c:\path\to\file.zip FileIwant.ext -d c:\place\to\put\it
    unzip.exe will remain the constant, the last 4 arguments however need to be changed at will....

    Any suggestions?
    The world is waiting. I must leave you now.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    Simple approach
    Code:
    char buff[1000];
    sprintf( buff, "d:\\unzip.exe %s -d %s >NUL", SourceFile, Destination);
    system( buff );
    Or use spawnv, like so
    Code:
    char *args[4];
    args[0] = SourceFile;
    args[1] = "-d";
    args[2] = Destination;
    args[3] = NULL;
    spawnv( P_WAIT, "d:\\unzip.exe", args );
    But this doesn't have redirection

  3. #3
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Thank you for your suggestions Salem.
    I will toy with those after a bit and post a reply with what I come up with.
    The world is waiting. I must leave you now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parameters quick Question
    By lifeis2evil in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2007, 11:12 PM
  2. function with variable number of parameters
    By mikahell in forum C++ Programming
    Replies: 3
    Last Post: 07-23-2006, 04:35 PM
  3. Additional parameters for operator delete
    By darksaidin in forum C++ Programming
    Replies: 0
    Last Post: 09-21-2003, 12:46 PM
  4. Passing parameters from VB to C++ through ActiveX DLL
    By torbjorn in forum Windows Programming
    Replies: 0
    Last Post: 12-10-2002, 03:13 AM
  5. command-line parameters.
    By Tombear in forum C Programming
    Replies: 2
    Last Post: 10-28-2001, 08:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21