Link to home
Start Free TrialLog in
Avatar of local_kidu
local_kidu

asked on

Urgent C Question ===> need fast replies

Hi Experts,
               Iam writing a command line program. My commnad looks like below

c:\commandname -f "c:\temp.txt" systemname:port GetStatus

The command is exactly same as shown above

I am able to get the whole command inside a string say dummy. Now I want is when a -f is encountered in the command then take

c:\temp.txt in string test1.
systemname:port in string test2.
GetStatus in string test3.

As Iam new to C language , iam not so versed in string manipulation function. So please provide me code for checking the above. and getting the substrings in the respetive strings.

Early responses are greatly appreciated. Code responses too.


Warm Regards..............

LK
Avatar of cookre
cookre
Flag of United States of America image

Which flavor C?

If you have a main with argc and argv, just parse the parms one at a time:

char CmdParms[10][256];
if (argc>10) say too many parms
for (argidx=0; argidx<argc; argidx++)
     {
     strcpy(CmdParms[argidx],argv[argidx])
     }
That will leave the exe reference in CmdParms[0] and parm n in CmdParms[n].

Of course, if your command line has a rigid structure, you can just work straight from argv.
Avatar of andyinlondon
andyinlondon

There are several ways to achieve this.

Either do this...

int main(int argc, char *argv[])
{
  int i;  // declare a loop counter
  char testStrings[3][256];  // declare an array of strings
  // test the number of arguments
  if (argc < 4)
  { // not enough arguments
    return 1;
  }
  if (argv[1][0] != '-' || argv[1][1] != 'f')
  { // -f not found
    return 1;
  }
  // -f found, copy the arguments into your strings
  for (i = 2; i < argc; i++)
  {
    strcpy(testStrings[i - 2], argv[i]);
  }
  return 0;
}

or this...

int main(int argc, char *argv[])
{
  char *test1, *test2, *test3;  // declare char pointers
  // test the number of arguments
  if (argc < 4)
  { // not enough arguments
    return 1;
  }
  if (argv[1][0] != '-' || argv[1][1] != 'f')
  { // -f not found
    return 1;
  }
  // -f found
  test1 = argv[2];
  test2 = argv[3];
  test3 = argv[4];
  return 0;
}
OOPS!
Lines reading "if (argc < 4)" should read "if (argc < 5)"
Avatar of local_kidu

ASKER

Dear andyinlondon,
                           I am not using argc or agrv , instead Iam getting the whole commnad inside a string(dummy), as I specified in the question. So I want the answer based on that.

LK
In that case, short of parsing the string yourself either character by charactrer or with strtok(), you can use CommandLineToArgvW():
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/commandlinetoargvw.asp

Note that the input string is Unocode.  If you used GetCommandLine(), it's already in Unicode, otherwise, you'll have to convert it.
ASKER CERTIFIED SOLUTION
Avatar of andyinlondon
andyinlondon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks andyinlondon, You have done a great work gor me. Thanks alot

LK