system() function problem

Hey guys sorry but I just started programming like 2 weeks ago, and I recently hit a snag with using system() function to run start a program.

It works just fine when referring to a file location that has no spaces.
ex. system("Drive/stuff/example/file.exe")

but I cannot get it to work with things that have spacing in the file names.
ex. system("C:/Program Files (x86)/RIFT Game/playrift.exe")

if anyone could help me understand why its not working or show me a better way of doing this (remembering I am a beginner) I would greatly appreciate it.
This is because the shell thinks that you're calling a program named RIFT with the argument Game/playrift.exe. If the Windows shell is anything like most *nix consoles, putting quotes around (EDIT: Actually, the entire string. Duoas is right.) should fix it.

Happy programming!

-Albatross
Last edited on
Put quotes around the entire string:

"\"C:/Program Files (x86)/RIFT Game/playrift.exe\""

BTW, you shouldn't hardcode paths like that one...
BTW, you probably shouldn't be using system. http://www.cplusplus.com/forum/articles/11153/
Besides, that looks more like something in a batch script ¿What are you trying to do?
I am just trying to get down a few different ideas but the program I am trying to make a program that acts as a list to start other programs, dumb and useless I know but I am a beginner.

so for instance I am making it like this

1. Game Name
2. Game Name
3. Game Name
4. Game Name
5. Game Name

Please select one.

THen you enter the number corresponding to the program you want to start, the only problem I have encountered is file location with a space in the naming.

to get practice I am setting this up as an If Then Else statement like a check list of if its 1 do this else if its 2 do this else etc.
Last edited on
That's a good project.

Hint: use a file that lists the game name and its start command. Something like this:

Pac-Man
"C:\Program Files\DOS Games\Pacman\pac.exe"

Jazz Jackrabbit
"C:\Program Files\DOSBox\dosbox.exe" C:\PROGRA~1\DOSGAM~1\Jazz\jazz.exe -exit -fullscreen

Bouncing Babies
"C:\Program Files\DOSBox\dosbox.exe" C:\PROGRA~1\DOSGAM~1\bbabies\baby.exe -exit -fullscreen -conf C:\PROGRA~1\DOSGAM~1\bbabies\baby_dosbox.conf

Etc.

Hope this helps.
Last edited on
Topic archived. No new replies allowed.