?
Solved

Want help in string manipulation

Posted on 2003-11-17
4
Medium Priority
?
313 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
[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
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 600 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses
Course of the Month13 days, 14 hours left to enroll

801 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