Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Parsing and separating the command line

Posted on 1997-08-21
4
Medium Priority
?
363 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 40 total points
ID: 1167910
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
ID: 1167911
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
ID: 1167912
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
ID: 1167913
You can use GetCommandLine, as well as argv in Win32.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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.

773 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