Solved

Urgent C Question ===> need fast replies

Posted on 2003-12-06
7
369 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
  • 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 

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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

This article will show, step by step, how to integrate R code into a R Sweave document
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

828 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