• C

Want help in string manipulation

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
google_manAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sunnycoderCommented:
> 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
sunnycoderCommented:
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
google_manAuthor Commented:
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
sunnycoderCommented:
>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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.