By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,500 Members | 1,769 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,500 IT Pros & Developers. It's quick & easy.

main argv command line

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Fri, 13 Aug 2004 20:45:07 +0200, Bernd Danberg <be***********@gmx.de>
wrote:
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
Jul 22 '05 #2

P: n/a
Bernd Danberg wrote:
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
Jul 22 '05 #3

P: n/a
Hi,
Bernd Danberg wrote:
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
Jul 22 '05 #4

P: n/a
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 Danberg wrote:
Hi,

Bernd Danberg wrote:
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

Jul 22 '05 #5

P: n/a
Hi,

<wi******@brambleton.net> schrieb im Newsbeitrag
news:2o************@uni-berlin.de...
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
Jul 22 '05 #6

P: n/a
On Fri, 13 Aug 2004 20:45:07 +0200, "Bernd Danberg"
<be***********@gmx.de> wrote:
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.
--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #7

P: n/a
Bernd Danberg wrote:
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

Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.