Solved

Want help in string manipulation

Posted on 2003-11-17
4
266 Views
Last Modified: 2010-08-05
Hi experts,
              I am writing a program which accepts command line arguments and process them. I am able to accept the string from the command line. The string is like

command -f "c:\temp.txt"  \servername:port\Getstatus

What I want is that is code helping to do the follwing things

> if a -f is noticed then take c:\temp.txt in a variable and pass it.
> Also the validation code for checking the correctioness of entry....(The command should be entered exactly in the format).
> Also I want servername:port seperately in another string....

Please help me in doing these things, urgent respondings is greatly appreciated

Thanks in advance

GM
0
Comment
Question by:google_man
  • 3
4 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9768889
> if a -f is noticed then take c:\temp.txt in a variable and pass it.
> Also the validation code for checking the correctioness of entry....(The command should be entered exactly in the format).
> Also I want servername:port seperately in another string....

>command -f "c:\temp.txt"  \servername:port\Getstatus


> if a -f is noticed then take c:\temp.txt in a variable and pass it.
char * arg1;

if ( strncmp ( argv[1] , "-f", 2 ) == )
        arg1 = argv[1] + 3;


> Also I want servername:port seperately in another string
char arg2[64];
char * temp;

temp = strrchr ( argv[2] , '\' );
strcpy ( arg2, argv[2]+1, temp - argv[2] );
arg2+(temp - argv[2] ) = '\0';

> Also the validation code for checking the correctioness of entry....(The command should be entered exactly in the format).
does that include the punctuation marks
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9768912
validation including punctuation
>command -f "c:\temp.txt"  \servername:port\Getstatus

if ( argc != 2 )
    /* wrong number of args */

if ( strncmp ( argv[1] , "-f\"", 3 ) != 0 )              ======== missed 0 last time
              /* ill formed argument 1*/

if ( argv[1] + (strlen (argv[1]) ) != '"' )
           /* ill formed argument 1*/

if ( *argv[2] != '\'   ||  strchr (argv[2],':' ) == NULL || strrchr (argv[2], '\' ) == argv[2] )
        /* ill formed argument2 */
0
 

Author Comment

by:google_man
ID: 9769512
Thanks very much for the code. Sorry for the late acceptance as I didn't got any mails regarding reply......

Also one more doubt...... If I get the whole command

"command -f "c:\temp.txt"  \servername:port\Getstatus"

in a string and then what to process it with above procedures then how will i have to proceed

eg:= Cstring dummy = command -f "c:\temp.txt"  \servername:port\Getstatus

Please solve this also. Iam giving you the points for the question
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 150 total points
ID: 9769560
>If I get the whole command

>"command -f "c:\temp.txt"  \servername:port\Getstatus"

>in a string and then what to process it with above procedures then how will i have to proceed

you have to proceed in a fashion similar to the one for command line args ... agrv[] would be replaced with the string name

>dummy = command -f "c:\temp.txt"  \servername:port\Getstatus

here I see a space between -f and ", which i had not considered in the previous post ... adjust code accordingly if that is the case

>if a -f is noticed then take c:\temp.txt in a variable and pass it.
previously we had

char * arg1;

if ( strncmp ( argv[1] , "-f", 2 ) == )
       arg1 = argv[1] + 3;

now it would be

if ( (arg1 = strstr ( dummy, "-f" ) ) != NULL )
         arg1 = arg1 + 4;

similar trivial modifications to other segments would get other cases working too ... read the man pages for strstr, strchr, strrchr, strcmp, strncmp, strlen
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

762 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

24 Experts available now in Live!

Get 1:1 Help Now