main argv command line

Discussion in 'C++' started by Bernd Danberg, Aug 13, 2004.

  1. 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
    Bernd Danberg, Aug 13, 2004
    #1
    1. Advertising

  2. On Fri, 13 Aug 2004 20:45:07 +0200, 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
    >


    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
    John Harrison, Aug 13, 2004
    #2
    1. Advertising

  3. Bernd Danberg

    Guest

    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
    , Aug 13, 2004
    #3
  4. 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
    Bernd Danberg, Aug 13, 2004
    #4
  5. Bernd Danberg

    Guest

    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
    >
    >
    , Aug 13, 2004
    #5
  6. On Fri, 13 Aug 2004 20:45:07 +0200, "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


    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
    Bob Hairgrove, Aug 13, 2004
    #6
  7. Hi,

    <> schrieb im Newsbeitrag
    news:...
    > 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
    Bernd Danberg, Aug 13, 2004
    #7
  8. Bernd Danberg

    Rich Grise Guest

    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
    Rich Grise, Aug 15, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bret

    char **argv vs. char* argv[]

    Bret, Aug 31, 2003, in forum: C Programming
    Replies:
    21
    Views:
    4,563
    Richard Heathfield
    Sep 3, 2003
  2. David
    Replies:
    10
    Views:
    5,926
    Richard Heathfield
    Sep 15, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,617
    Old Wolf
    Jan 20, 2004
  4. =?ISO-8859-1?Q?Thomas_N=FCcker?=

    sys.argv[0] - 'module' object has no attribute 'argv'

    =?ISO-8859-1?Q?Thomas_N=FCcker?=, Jun 30, 2003, in forum: Python
    Replies:
    0
    Views:
    888
    =?ISO-8859-1?Q?Thomas_N=FCcker?=
    Jun 30, 2003
  5. jab3

    char **argv & char *argv[]

    jab3, Dec 4, 2004, in forum: C Programming
    Replies:
    5
    Views:
    653
    Chris Torek
    Dec 8, 2004
Loading...

Share This Page