-
The sytem(); function
Hello anyone who is reading this. The question I have seems simple, and may be. I just doint' know. Okay, so all I want to do is have my program open up a batch file on my desktop. The command would be like CD Desktop [enter] START batch.bat [enter]. Only, I can't seem to get to the desktop to open it up. What I was doing was something like this:
Code:
System("Cd Desktop");
Now, this doesn't work obviously. But basically what I am asking is either how to navigate and then open it. Or, how to make two "system();" functions connected, as to make one CD Desktop and the other START batch.bat. This may not be possible at all. But I think you guys will understand what I am trying to do.
Thanks!
-
Why not this?
Code:
system("path/to/file/file.bat");
-
Ah, good idea, I'll try it. Anyone else got any other ways you know it would work?
-
Nope, that did not work. Due to the spaces. I remember making a program once that played an mp3 in the same directory on Ubuntu, but ran into the same problem. The only fix I found was to rename the file w/o any spaces. So who knows how to fix it now? haha just another problem. Oh and the path that is messing it up is "Documents and Settings"......lame
-
Quotes, or escape the spaces,
ie, "cd C:\Documents and Settings\blah" becomes "cd \"C:\Documents and Settings\blah\""
What does your program do? There's usually better ways than using system() :)
-
Code:
system("\"c:\\path with spaces\\file.bat\"); /* ? */
-
Ha! thanks Dave, and it was the combination of things so zac you were just missing the double "\\." Anyway, I basically just figured out how to do batch, and wanted a program to open a program. Anyways what is your better way of doing it than using system() zac?
-
If you want a program to open a program? I'd use CreateProcess().
-
Well, I say that lightly. I wanted it to open a Batch file. Which isn't really a program CreateProcess looks like it is .exe's.
-
system() is probably fine for this sort of thing. CreateProcess() lets you "open" .txt files and folders and so on, using whatever the default program is. You can do this with system, too, by using "start".
Code:
system("start readme.txt");
Opening .bat or .exe or .com files can be done with either function, however, with no special finangling.
-
Okay, so now that we have gotten through that problem, another question has arrised. How can I tell i there was an error in the system(); function. Because as far as I can tell, it doesn't return anything. Now I know it will pop up with a box that is like You Suck! But I mean like so I can base an if statement on whether or not it was able to open the .bat successfully or not. Help please?
-
You could always wrap an "if statement" around your system() call, couldn't you?
-
Well, what good would that do? I want to be able to see if the system(); function worked, if so I want something to happen. I don't see how an if statement would work. Maybe I am wrong. Explain it if you know it works.
-
system returns non-zero value if it failed
system returns return value of the program when succeded
if the program itsefl returns non-zero value - it is slightly hard to distinguish between failed system call and failed program
-
Ah, O.K. thanks. I didn't know it had a return value.