The sytem(); function

This is a discussion on The sytem(); function within the C Programming forums, part of the General Programming Boards category; Hello anyone who is reading this. The question I have seems simple, and may be. I just doint' know. Okay, ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103

    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!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Why not this?
    Code:
    system("path/to/file/file.bat");
    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
    Jul 2007
    Location
    Texas
    Posts
    103
    Ah, good idea, I'll try it. Anyone else got any other ways you know it would work?

  4. #4
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    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

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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()

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    system("\"c:\\path with spaces\\file.bat\"); /* ? */
    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.*

  7. #7
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    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?

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    If you want a program to open a program? I'd use CreateProcess().

  9. #9
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    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.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    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.
    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.

  11. #11
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    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?

  12. #12
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    You could always wrap an "if statement" around your system() call, couldn't you?

  13. #13
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    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.

  14. #14
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  15. #15
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    Ah, O.K. thanks. I didn't know it had a return value.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21