Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Urgent C Question ===> need fast replies

Posted on 2003-12-06
7
Medium Priority
?
376 Views
Last Modified: 2010-04-17
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
Comment
Question by:local_kidu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 22

Expert Comment

by:cookre
ID: 9889474
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
 

Expert Comment

by:andyinlondon
ID: 9892355
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
 

Expert Comment

by:andyinlondon
ID: 9892370
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.

 

Author Comment

by:local_kidu
ID: 9894615
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
 
LVL 22

Expert Comment

by:cookre
ID: 9894946
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
 

Accepted Solution

by:
andyinlondon earned 400 total points
ID: 9895747
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
 

Author Comment

by:local_kidu
ID: 9895796
Thanks andyinlondon, You have done a great work gor me. Thanks alot

LK
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Simple Linear Regression

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question