Solved

Parsing and separating the command line

Posted on 1997-08-21
4
358 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
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

749 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