Solved

Urgent C Question ===> need fast replies

Posted on 2003-12-06
7
365 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
Comment Utility
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
Comment Utility
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
Comment Utility
OOPS!
Lines reading "if (argc < 4)" should read "if (argc < 5)"
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:local_kidu
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks andyinlondon, You have done a great work gor me. Thanks alot

LK
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

A short article about a problem I had getting the GPS LocationListener working.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now