Thread: start a program with space in the path

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    3

    start a program with space in the path

    How do I start a program from a C program whose path is has a space in it?

    For example, lots of applications are installed in C:\Program Files\...,

    I've tried "system()", but it thinks "C:\Program" is the command name,

    The "spawn" functions don't like the space in the path either.

    Thanks,

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Perhaps they don't like the \P?

    Post your code.

    Use forward slashes in string literals if you prefer, otherwise escape a backslash with a backslash.

    Enclose the whole string within double-quotes (which each may need to be escaped if used in a string literal).
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    3
    The line is: _spawnl(_P_WAIT, CL, CL, buffer, NULL);

    If CL is "c:\devstudio\vc98\bin\cl.exe", the spawnl() works. (VC is not installed in the default directory.)

    If CL is "c:\Program Files\Microsoft Visual Studio\VC98\bin\cl.exe", then spawnl() won't work.

    The "buffer" contains the rest of the parameters.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Actual code really does help.

    But does this work?
    Code:
    ""c:\Program Files\Microsoft Visual Studio\VC98\bin\cl.exe""
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    3

    Unhappy

    nope. :-(

    Tried it before I posted.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Use "" only for argv[0]. Following is working:
    Code:
    int main(int arc, char* argv[])
    {
    	char CL[] = "d:\\1 1\\N.EXE";
    	char CL2[] = "\"d:\\1 1\\N.EXE\"";
    	 _spawnl(_P_WAIT, CL, CL2, 0);
    	 return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    For system() Dave_Sinklula's solution should work, though.
    Code:
    system("\"\\program files\\mozilla firefox\\firefox\"");
    Alternatively, use the DOS name for the folder.
    Code:
    system("\\progra~1\\mozill~1\\firefox");
    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. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. a space in a program path
    By eam in forum C Programming
    Replies: 2
    Last Post: 10-26-2003, 10:19 PM
  4. Start up program
    By Breetai in forum Windows Programming
    Replies: 2
    Last Post: 01-11-2003, 01:12 PM
  5. Start a program
    By FunkeeMunkee in forum C++ Programming
    Replies: 1
    Last Post: 08-26-2001, 07:18 PM