Solved

Urgent C Question ===> need fast replies

Posted on 2003-12-06
7
370 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
Independent Software Vendors: 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!

 

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 100 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

749 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