473,396 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

main argv command line

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
7 9858
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: qazmlp | last post by:
void func() { // Is it by anyway possible to read the value of the first command line parameter i.e. argv here ? } int main() { func() ; // No command line arguments are passed to func(). }
18
by: Jeff Rodriguez | last post by:
If main is prototyped as: int main(int argc, char *argv); You will end up with a bunch of arguments in *argv, and the number in argc. Now what I want to do is emulate that same action on a...
16
by: Stephen Mayes | last post by:
Correct me if I am mistaken. The C standard guarantees that in a C hosted environment we can define main as: int main( int argc, char *argv ). I always assume that 'argc' is the number of...
12
by: Bill Cunningham | last post by:
> #include <stdlib.h> > #include <stdio.h> > > int main(int argc, char *argv) { > FILE *in, *out; > > if(argc==1) > { > in = stdin; > out = stdout;
26
by: Bill Cunningham | last post by:
I've read that the only parameters that main takes other than a void is this main(int argc,char *argv) I may be correct on this. I want to take from the command line prompt 2 doubles, pass the...
75
by: Beni | last post by:
I have been programming in C for about a year now. It sounds silly, but I never took the time to question why a C(or C++ or Java) program execution begins only at the main(). Is it a convention or...
6
by: fatwallet961 | last post by:
is the main function in python is exact compare to Java main method? all execution start in main which may takes arguments? like the follow example script - def main(argv): ...
4
by: interec | last post by:
Hi Folks, I am writing a c++ program on redhat linux using main(int argc, wchar_t *argv). $LANG on console is set to "en_US.UTF-8". g++ compiler version is 3.4.6. Q1. what is the encoding of...
2
by: significantBit | last post by:
n00b here. Just started learning C a couple of days ago. I'm using xcode alternatively with emacs. My question is in regards to the main function. Everytime I create a project (standard...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.