Solved

Parsing and separating the command line

Posted on 1997-08-21
4
355 Views
Last Modified: 2006-11-17
Yeah, this is supposed to be an easy task, but I'm having difficulties. This is a Windows program, so the command line doesn't get automatically parsed like it does in DOS programs. here's the code I have so far:

char *pdest;
int result;
int Spc = ' ';
char *Find;
pdest = strchr(szCmdLine, Spc);
result = pdest - szCmdLine + 1;

szCmdLine is a char * that contains the command line. so far I have   result   storing the position of a space, but what I need now is to store the characters up to the space into a char * and the characters after the space into another char *. I haven't found the substring functions that would be equivalent to Visual Basic's Left(string, length), Right(string, length), and Mid(string, start, length). I'm looking for a code snippet that would accomplish this stuff in C++
0
Comment
Question by:mnichols
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
md041797 earned 10 total points
Comment Utility
You need strtok.

#include <string.h>
char *strtok(char *s1, const char *s2);
wchar_t *wcstok(wchar_t *s1, const wchar_t *s2);
char far * far _fstrtok(char far *s1, const char far *s2)

#include <mbstring.h>
unsigned char *_mbstok(unsigned char *s1, const unsigned char *s2);

Description

Searches one string for tokens, which are separated by delimiters defined in a second string.
strtok considers the string s1 to consist of a sequence of zero or more text tokens, separated by spans of one or more characters from the separator string s2.
The first call to strtok returns a pointer to the first character of the first token in s1 and writes a null character into s1 immediately following the returned token. Subsequent calls with null for the first argument will work through the string s1 in this way, until no tokens remain.

The separator string, s2, can be different from call to call.

Note:      Calls to strtok cannot be nested with a function call that also uses strtok. Doing so will causes an endless loop.

Return Value

strtok returns a pointer to the token found in s1. A NULL pointer is returned when there are no more tokens.
0
 

Expert Comment

by:nigel5
Comment Utility
If you are using Visual C++ (at least), __argv, and __argc can be used where you would normally use the main(int, char*[]).

(double underscore)
0
 
LVL 1

Author Comment

by:mnichols
Comment Utility
Actually, this comment is for nigel5...
I thought that argv and argc were only applicable to Win32 console programs. I'm making a Win32 application, with a WinMain function instead of a Main function...
-- Matt
0
 
LVL 4

Expert Comment

by:md041797
Comment Utility
You can use GetCommandLine, as well as argv in Win32.
0

Featured Post

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

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 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

13 Experts available now in Live!

Get 1:1 Help Now