Thread: command line asterisk converted to list

  1. #1
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198

    command line asterisk converted to list

    I am writing a program to take 3 command line arguments and perform simple arithmetic. The second argument is the operator, and the first and third are operands. They all work fine except for the asterisk. It is converted to a list of files in the current directory. I am working under the command line in Windows XP. Does anybody know of a way to fix this problem and have an asterisk just represent an asterisk? Thanks.

    Code:
    #include <iostream>
    using namespace std;
    
    int main(int argc, char* argv[])
    {
        for (int i = 0; i < argc; i++)
        {
            cout << argv[i] << endl;
        }
        return 0;
    }
    
    /*
    Test Run:
    c:\test>test.exe 5 * 8
    test.exe
    5
    test.cpp
    test.exe
    8
    
    where test folder contains test.cpp and test.exe
    */
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Strange. When I run that program (on XP) from command prompt this is what I get:
    Code:
    > test 5 * 8
    test
    5
    *
    8
    Which is what I would expect
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    > test.exe "5 * 8"
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I am working under the command line in Windows XP
    But which compiler?

    Wildcard expansion of the command line (or globbing) can be turned on or off at various points, depending on your shell / OS / compiler ....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    Well actually I discovered the problem working in Java using the most current javac compiler. But I also tested this in the most current version of Dev-Cpp (4.9.9.2). Might there be a way to disable it at the command line? Or is it something I'd have to write special code to handle?
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I think it's a feature of the shell, not of your program.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    If that's the case, then I have two questions:
    1. Why does it do the same thing when I run my java program from the IDE and just set the command line parameters? It isn't using the command line shell is it?
    2. How can I change the behavior of the shell? Or is it not possible?
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    1. Why does it do the same thing when I run my java program from the IDE and just set the command line parameters? It isn't using the command line shell is it?
    What I mean is, whenever a program is launched, * is expanded. Try *
    Code:
    system("program *");
    [edit] Yes, it does use the shell. Every time you evoke a program, you use the shell. (At least that's what I've always thought.) [/edit]
    2. How can I change the behavior of the shell? Or is it not possible?
    I don't know much about that.

    But it must be possible: Try this:
    Code:
    C:\>echo *
    On my non-windows XP computer that echos "*".

    I would suggest making the program take as arguments "5*4", with no spaces, or else read input from stdin; you could provide an equation like this:
    Code:
    C:\>echo 5+4 > program
    [edit=2] * Of course, you shouldn't do this. [/edit]
    Last edited by dwks; 07-14-2006 at 01:43 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> system("program *");

    Wow, somebody's recommending a system command ... egad. But what happens if someone makes a batch program called program which is made to delete the hard drive? You should think of the consequences dwks!!! Only joking. Sorry. I feel rightly ashamed.

  10. #10
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    Well it sounds like this is more of a problem than I thought it was. Right now I'm not worried about hacking together a solution just so that my simple arithmetic problem will work without me needing to use quotations around an asterisk. That's much simpler. Thank you for your help, I have learned a lot from this.
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  11. #11
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    The way I'd do it is have a string as the second arguement (like what dwks said), like: 5*3, then you can parse (is that the right word), it out, and attack the sum that way.

    >> [edit=2] * Of course, you shouldn't do this. [/edit]
    hehe

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    5*3 still won't work if there's a file called 5foo3 in the current directory. Then it'll expand just like any other pattern.

    To be really safe, put a character in the argument, such as ':', that can't exist in a file. (Of course, it can exist in a Linux filename, but under Linux you'd just use single quotes around the parameter, and the shell won't do globbing. That's what it's called; globbing.)

    [edit]
    (From signature)
    I like hiding things
    What you should do is look at the source for a typical CBoard page and find out what the background colour is, and set that text to that.

    Sorry for spoiling your signature.
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    I just understood the problem. You could always replace the Astrix (*) with a x or a X

  14. #14
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    I hate you!

    EDIT: Sorry for the double bump! My anger flamed the passionate hatrid I now feel towards dwks! :P

    it used to be 'PM me if you see this', then 'Gone unnoticed for weeks', then 'Gone unnoticed for months', and at the start there was no hidden message there.

    EDIT 2: I like magenta. Changed it
    Last edited by twomers; 07-14-2006 at 02:30 PM.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    "Hiding in plain sight." That's what you should make it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM