dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: [RESOLVED] Passing program arguments

Hybrid View

  1. #1
    Join Date
    Feb 2005
    Location
    Denver
    Posts
    353

    [RESOLVED] Passing program arguments

    This is probably more of a unix bash shell question, but I didn't know where else to post. My apologies if this is the wrong forum for this question.

    Here is a file with the following contents (4 lines, let's call the file f)...

    hello
    a b c
    d e f
    'x y z'


    Note the double and single quotes on some of the lines.

    Here's a C++ program (let's call it nargs)...

    Code:
    #include <iostream>
    int main (int argv, char* argc [])
    {
       std::cout << \n argv =  << argv << std::endl;
    }
    1) Executing the following command from the bash shell will output the following...

    $ while read line; do printf $line ; done < f
    hello a b c d e f 'x y z'


    2) Executing the following command from the bash shell will output the following...

    $ nargs hello a b c d e f 'x y z'

    argv = 7


    3) Executing the following command from the bash shell will output the following...

    $ nargs $(while read line; do printf $line ; done < f)

    argv = 11


    What is going on here? I know the shell is performing some kind of translation regarding the quotes, but why the inconsistency? In #1, the quotes are present in the output. In #2, I've physically typed the quotes, just like they've appeared in #1. In #3, the quotes are obviously gone since the C++ program received 11 arguments, vice 7 in #2.

    I've also tried using backslashes between the spaces instead of quotes. This doesn't work either. Is there a way to get around this problem? I haven't run across any shell settings to solve this.

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,151

    Re: Passing program arguments

    Try:
    Code:
    cat f | xargs nargs
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Feb 2005
    Location
    Denver
    Posts
    353

    Re: Passing program arguments

    Quote Originally Posted by Marc G View Post
    Try:
    Code:
    cat f | xargs nargs
    Thank you for the response. Although my post wasn't my exact scenario, I tried to simplify it as much as possible to get to the root of the problem. And yes, your answer not only solved my post, but applying the same idea to my actual problem was solved as well. Thanks again.

    I do have another problem related to a very similar idea using xargs, but since no C++ coding is involved, I'll go and post it on a unix forum.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)