PDA

View Full Version : system() question



HokeyFry
December 27th, 2006, 11:24 AM
im making a program in C++ that basically will run mpg321 or 123 (user chooses) and loop-play a folder X number of times. when i tell it to play through the folder once, this is what it passes to system(char*) :


mpgXXX *.mp3

and this runs fine. however... when i tell it to loop 2 or more times the string i pass looks somewhat like this:


mpgXXX *.mp3 *.mp3 *.mp3


and it says that *.mp3 cannot be found. I dont understand why this happens when running "mpgXXX *.mp3 *.mp3 *.mp3" from the terminal manually will successfully loop three times.

po0f
December 27th, 2006, 11:40 PM
HokeyFry,

Can you not just loop it like this? (Assuming it's C):

int i;
for (i = 0; i < num_loops; i++)
system("mpgXXX *.mp3");

HokeyFry
December 28th, 2006, 08:08 AM
i get the same error. i did it the way i mentioned because i got the error first doing it your way.

po0f
December 28th, 2006, 08:17 AM
HokeyFry,

Have you tried using exec() (http://www.netadmintools.com/html/3exec.man.html)?

Wybiral
December 28th, 2006, 08:18 AM
Could you post the actual function or chunk of code, maybe it's a little error or something in the C++ code. Not sure how... But I don't see why the for loop wouldn't have worked.

amo-ej1
December 28th, 2006, 05:52 PM
quoting man system


system() executes a command specified in command by calling /bin/sh -c
command, and returns after the command has been completed. During exe‐
cution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT
will be ignored.


So this can be reproduced from commandline:



edb@rogue:~$ ls * * * | wc -l
114
edb@rogue:~$ ls -d * * * | wc -l
18
edb@rogue:~$ /bin/sh -c ls * * * | wc -l
6


so in my opinion it relates to environment of the 'regular' bash shell.


anyhow, the task you're trying to do smell like shellscripting, it doesn't smell like C/C++ at all ;)

HokeyFry
February 17th, 2007, 01:31 AM
yeah it would be better for a shell script i kinda knew that but i didnt have the resources at the time to learn scripting but i know basic C++ pretty well so...... yeah. but thanks for all of the responses