Skip to main content
gamedev.net
🔒 Locked

Executing a command line from the code

Started by Mari_p Sep 8, 2006 at 8:47 AM 5 replies 1.4k views
Original Post
Mari_p
Mari_p
Hi friends, Two basic questions: 1) How to execute a command line from the code (C/C++)? For example, I would like to execute a command line like this: "lame --alt-preset standard - song.mp3" 2) Please, look the following codes: ------------------------------------------

CODE 1:
LPSTR MyString(LPSTR buffer)
{
   _itoa(timeGetTime(), buffer, 10 );
   return buffer;
}

char buffer[20] = "";
printf("%s", MyString(buffer));
------------------------------------------

CODE 2:
LPSTR MyString()
{
   char buffer[20] = "";
   _itoa(timeGetTime(), buffer, 10 );
   return buffer;
}

printf("%s", MyString());
------------------------------------------ CODE 1 works, but CODE 2 doesn't work... Why? Thanks in advance
ToohrVyk
ToohrVyk
1° Use system or exec, depending on your needs.

2° You're returning a pointer to local memory, which is a Bad ThingTM.
Omid Ghavami
Omid Ghavami
1) system

2) Code two doesn't work because you allocate buffer on the stack, and then return a pointer to it. The pointer will be invalid as soon the function ends.

EDIT: ah, too slow [dead]
Best regards, Omid
Gorwooken
Gorwooken
If you want to play the mp3 in the default windows mp3 player, you should use ShellExecute

It performs the same function as if you were to type something into Start->Run
Kwizatz
Kwizatz
On Windows look for CreateProcess(), WaitForSingleObject() and CloseHandle().

On *nix look for fork(),waitpid() and execl().
Zahlman
Zahlman
0) If you have "basic" questions, there's always For Beginners. They put it at the top of the forum list for a reason - so that people will notice it.

1) There's no such thing as "C/C++". There are things that are common to C and C++, and "executing commands" is one of them, but (a) if you intend to use C++, you should normally use C++-specific constructs for things where they exist; and (b) these days, you'd better have a damn good reason for using C at all.

2) Following up on the "C++-specific constructs" tip, please forget you ever heard such horrible, unseemly, naughty words as "printf", "LPSTR" or "_itoa".

#include <string>#include <sstream>#include <iostream>std::string MyString() {  std::stringstream ss;  ss << timeGetTime();  return ss.str();}std::cout << MyString();// You actually can just std::cout << timeGetTime(), just as you could have// done printf("%d", timeGetTime()).


Please notice several things:

a) No version of atoi() (and certainly not _atoi()) is even standard, for C or C++.
b) This way avoids any kind of problems with the memory management (which you clearly don't understand how to do properly; and which experts *correctly* avoid as much as possible *even though* they can do it properly), because the std::string is a *real object* that is self-contained and can be treated just like a primitive type.
c) The stream interfaces are type-safe; you never have to match up any magical formatting characters to the type of what you're outputting, PLUS you can implement them for any struct or class that you later create.
d) Streams work polymorphically; you never have to pick between printf, fprintf or sprintf (or similarly for other I/O functions). This is actually exploited for the purpose of implementing MyString(), and this approach allows for much better error checking, again because the streams are represented by real objects. For example, with atoi() there is never any way to know whether the input string was "0" or not a number at all. With a stream, in the first case, the target int is set to 0, and in the second case, the target int is *left alone*, plus the stream's internal state is modified in a way that you can (and should) check for. That's the default behaviour, though; if you prefer, you can easily configure it so that in the second case it throws an exception instead.

3) If invoking other programs is the primary purpose of your program, you should instead be using a scripting language anyway.
Mari_p
Mari_p
Thanks for all replies.

I decided to use CreateProcess() due to its flexibility.

Zahlman, sorry if my post was uncomfortable. Anyway, thanks for the explanations.

[Edited by - Mari_p on September 10, 2006 7:00:43 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.