Sign in to follow this  
Mari_p

Executing a command line from the code

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this