Menu

Passing strings

2009-11-07
2012-09-26
  • Chris Davidson

    Chris Davidson - 2009-11-07

    I have a bat file I am using to pass a directory name as a string to a C++
    program. It all works fine unless there is a space in the directory name then
    it only sees the string to the first space. For example if I pass
    "C:\imagesort\unsorted images" as the directory, it sees the directory as
    "C:\imagesort\unsorted\" and leaves out the "images" part. Is there amoe easy
    way for C++ to see the whole string? Thanks in advance.

     
  • cpns

    cpns - 2009-11-08

    This is not an issue with Dev-C++, or even C++. It is a matter for the
    operating system. For command line arguments, space is a delimiter.

    The solution is to enclose the path in quotes, and the OS will pass it as a
    single argument.

    myprogram "C:\imagesort\unsorted images"

    Clifford

     
  • Chris Davidson

    Chris Davidson - 2009-11-08

    I tried doing that and I still only get part of the directory. I only get
    "C:\imagesort\unsorted." as what is passed. Here is what I have in the code
    for main:

    int main(int argc, char* argv)
    {
    stringstream ss;
    string nameOfDirectory;
    ss << argv;
    ss >> nameOfDirectory;

    nameOfDirectory = nameOfDirectory + "\";
    cout << nameOfDirectory << endl;

    }

    What am I doing wrong? I am sure it is in the argument passing but I am not
    sure what I need to do.

     
  • cpns

    cpns - 2009-11-08

    Read the documentation for std::stringstream, specifically for . You will
    observe that white-space is a delimiter for formatted input.

    your code is overcomplicated in any case; you can initialise a std::string
    with a C-string, so there is no need for all that stringstream nonsense.

    int main(int argc, char** argv)
    {
    string nameOfDirectory( argv ) ;

    nameOfDirectory = nameOfDirectory + "\";
    cout << nameOfDirectory << endl;
    }

    Also use the code markup correctly. Paste the text, select it, and click the
    code mark-up button to stop the code being rendered unreadable! Use the
    Preview pane beneath the message box to see how your post will end up. Then
    read and . Unfortunately SourceForge's markup is still buggy, but can do
    better than your post.

    : http://www.cplusplus.com/reference/iostream/ostream/operator%3C%3C/
    : http://www.codinghorror.com/blog/archives/001306.html
    : http://daringfireball.net/projects/markdown/syntax

     
  • Chris Davidson

    Chris Davidson - 2009-11-08

    Thanks for the code, it worked like a charm. Sorry ofr the posting of the
    code, next time I will use the code mark-up button.

     
  • cpns

    cpns - 2009-11-08

    Sorry ofr the posting of the code,
    next time I will use the code mark-up
    button.

    That is ok, for years Sourceforge has suffered from lack of code mark-up, and
    then when they finally implement, no one uses it, and the result can be worse
    than no mark-up (because the mark-down syntax uses some very simple constructs
    that tend to exist in plain text in any case - such as indent four spaces for
    code). So I am on a one-man campaign to promote its correct use!

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.