main argv command line

B

Bernd Danberg

Hi,

I have a real strange problem with the command line arguments given to the
main-function and together with using std::string:

#include <string>
int _tmain(int argc, _TCHAR* argv[])
{
std::string str(argv[1]);
println(str.c_str());
}

When I call now this simple and small programm using the following
command-line argument: "C\:dir2html lang" then my string str holds
non-useful-data. When I delete the space or the backslash then the string
contains the correct value. Any ideas what I am doing wrong in converting
the argument from the main function argv[1] to my string-object

Thanks in advance,
Bernd
 
J

John Harrison

Hi,

I have a real strange problem with the command line arguments given to
the
main-function and together with using std::string:

#include <string>
int _tmain(int argc, _TCHAR* argv[])
{
std::string str(argv[1]);
println(str.c_str());
}

When I call now this simple and small programm using the following
command-line argument: "C\:dir2html lang" then my string str holds
non-useful-data. When I delete the space or the backslash then the string
contains the correct value. Any ideas what I am doing wrong in converting
the argument from the main function argv[1] to my string-object

Thanks in advance,
Bernd

You are not doing anything wrong from a C++ point of view, except
pointlessly using non-standard features such as _tmain, TCHAR and println.
None of that explains the problem you are having however. If it is a real
problem then it is probably something to do with the operating system or
your environment, but not C++.

Why not print the values of argc and the argv array directly, instead of
messing with std::string? Whatever the problem is, it is nothing to do
with std::string.

john
 
W

williams

Bernd said:
Hi,

I have a real strange problem with the command line arguments given to the
main-function and together with using std::string:

#include <string>
int _tmain(int argc, _TCHAR* argv[])
{
std::string str(argv[1]);
println(str.c_str());
}

When I call now this simple and small programm using the following
command-line argument: "C\:dir2html lang" then my string str holds
non-useful-data. When I delete the space or the backslash then the string
contains the correct value. Any ideas what I am doing wrong in converting
the argument from the main function argv[1] to my string-object

Thanks in advance,
Bernd

Are you quoting the command line arg? The "lang" token may be
in arg[2]. What is the value of argc?

Robb
 
B

Bernd Danberg

Hi,
Bernd said:
Hi,

I have a real strange problem with the command line arguments given to the
main-function and together with using std::string:

#include <string>
int _tmain(int argc, _TCHAR* argv[])
{
std::string str(argv[1]);
println(str.c_str());
}

When I call now this simple and small programm using the following
command-line argument: "C\:dir2html lang" then my string str holds
non-useful-data. When I delete the space or the backslash then the string
contains the correct value. Any ideas what I am doing wrong in converting
the argument from the main function argv[1] to my string-object

Thanks in advance,
Bernd

Are you quoting the command line arg? The "lang" token may be
in arg[2]. What is the value of argc?
Yes I am quoting the command line arg. Exactly that's the command line
argument: "C\:dir2html lang". argc is 2.

Greetings,
Bermd
 
W

williams

Ok, was a shot in the dark. The following code works as expected (VS 6).
What is 'println' function? Maybe try printf.

#include "stdafx.h"
#include <string>

int main(int argc, char* argv[])
{
std::string str(argv[1]);
printf("%s\n", str.c_str());
return 0;
}

Robb

Bernd said:
Hi,

Bernd said:
Hi,

I have a real strange problem with the command line arguments given to
the
main-function and together with using std::string:

#include <string>
int _tmain(int argc, _TCHAR* argv[])
{
std::string str(argv[1]);
println(str.c_str());
}

When I call now this simple and small programm using the following
command-line argument: "C\:dir2html lang" then my string str holds
non-useful-data. When I delete the space or the backslash then the
string
contains the correct value. Any ideas what I am doing wrong in
converting
the argument from the main function argv[1] to my string-object

Thanks in advance,
Bernd

Are you quoting the command line arg? The "lang" token may be
in arg[2]. What is the value of argc?

Yes I am quoting the command line arg. Exactly that's the command line
argument: "C\:dir2html lang". argc is 2.

Greetings,
Bermd
 
B

Bob Hairgrove

Hi,

I have a real strange problem with the command line arguments given to the
main-function and together with using std::string:

#include <string>
int _tmain(int argc, _TCHAR* argv[])
{
std::string str(argv[1]);
println(str.c_str());
}

When I call now this simple and small programm using the following
command-line argument: "C\:dir2html lang" then my string str holds
non-useful-data. When I delete the space or the backslash then the string
contains the correct value. Any ideas what I am doing wrong in converting
the argument from the main function argv[1] to my string-object

The only reason I can think of is that you are compiling a Unicode
version of your program, in which case you need to use std::wstring
and not std::string.
 
B

Bernd Danberg

Hi,

Ok, was a shot in the dark. The following code works as expected (VS 6).
What is 'println' function? Maybe try printf.

#include "stdafx.h"
#include <string>

int main(int argc, char* argv[])
{
std::string str(argv[1]);
printf("%s\n", str.c_str());
return 0;
}

Robb
Yes you are right.... I wanted to write printf... Sadly I had to do too much
java stuff last time ;). It seems that it is only an error with my compiler
and debugging representation. I don't know exactly why, but now it works.
Thanks!

Bye,
Bernd
 
R

Rich Grise

Bernd said:
Hi,

I have a real strange problem with the command line arguments given to the
main-function and together with using std::string:

#include <string>
int _tmain(int argc, _TCHAR* argv[])
{
std::string str(argv[1]);
println(str.c_str());
}

When I call now this simple and small programm using the following
command-line argument: "C\:dir2html lang" then my string str holds
non-useful-data. When I delete the space or the backslash then the string
contains the correct value. Any ideas what I am doing wrong in converting
the argument from the main function argv[1] to my string-object

What, exactly, does "C\:dir2html" mean? On DOS boxen, I've seen "C:\dirfoo".
Are you escaping the colon?

Good Luck!
Rich
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top