Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 377
  • Last Modified:

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
0
local_kidu
Asked:
local_kidu
  • 3
  • 2
  • 2
1 Solution
 
cookreCommented:
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.
0
 
andyinlondonCommented:
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;
}
0
 
andyinlondonCommented:
OOPS!
Lines reading "if (argc < 4)" should read "if (argc < 5)"
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
local_kiduAuthor Commented:
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
0
 
cookreCommented:
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.
0
 
andyinlondonCommented:
Ok LK
Try this code instead.


#include <stdio.h>
#include <string.h>

#define NUM_ARGS  3
char yourDUMMYstring[] = "c:\\commandname -f \"c:\\temp.txt\" systemname:port GetStatus";

char *getNextArg(char **str);

void main(void)
{ int i;
  char *cp, *ptr;
  char test[NUM_ARGS][256];
  // test for -f
  if ((cp = strstr(yourDUMMYstring, "-f")) != NULL)
  { // -f found
    cp += 2;  // move pointer past the -f
    for (i = 0; i < NUM_ARGS; i++)
    { if (!(ptr = getNextArg(&cp)))
      { printf("\nWHOOPS!  arg %d missing\n", i);
        return;
      }
      // copy the arg into test array
      strcpy(test[i], ptr);
      // display it
      printf("test[%i] = %s\n", i, test[i]);
    }
  }
  getchar(); // wait for user to press <CR>
}

/* This function is interesting in that the parameter 'str' is a pointer to a pointer. 'str' is used as
 * both an 'IN' and an 'OUT' parameter; supplying the function with the string to parse, and returning
 * the remains of the string after the argument has been extracted.
 * Because it is necessary (and convenient) for this function to update the input string (otherwise the
 * same string would be found every time), you have two choices: either create a buffer, fill it with
 * the argument string and shift the remains of the string to the beginning of 'str', or employ the
 * pointer to pointer method which can return the updated pointer position itself.
 * The problem with the first method is that it involves a string moving operation which is costly in
 * terms of processing time, whereas the pointer to pointer method doesn't - but it's more complicated.
 * The function returns the argument (if found), otherwise it returns the NULL pointer.
 */
char *getNextArg(char **str)
{ char *cp, *strStart = *str;
  // ignore whitespace before arg
  for (; *strStart && *strStart <= ' '; strStart++);
  if (*strStart == '"')
  { // arg is enclosed in quotes
    strStart++; // not interested in the quote character
    // find the end of the arg
    if ((cp = strchr(strStart, '"')) == NULL)
    { // WHOOPS!  no closing quote found
      return NULL;
    }
  }
  else
  { // arg is not enclosed in quotes
    // find the end of the arg
    for (cp = strStart; *cp && *cp > ' '; cp++);
  }
  // replace quote or space with the string terminator
  if (*cp)
  { //
    *cp++ = '\0';
  }
  *str = cp;
  return strStart;
}
0
 
local_kiduAuthor Commented:
Thanks andyinlondon, You have done a great work gor me. Thanks alot

LK
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now