?
Solved

Parsing and separating the command line

Posted on 1997-08-21
4
Medium Priority
?
360 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
[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
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

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