2 Replies - 304 Views - Last Post: 05 August 2011 - 10:47 AM Rate Topic: -----

#1 Mr_GriM  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-August 11

Char array with fixed value = fine, variable entry NO.

Posted 05 August 2011 - 10:13 AM

Hi,

I'm getting some behaviour in a program I can't explain that probably has an obvious answer, here goes:

I'm using this block of code I found to play .mp3 tracks (Linux):

       pid_t x;      // a special kind of int
        char kil[20] = "kill -s 9 ";

        x = fork();   /* now there's actually two "x"s:
if fork succeeds, "x" to the CHILD PROCESS is the return value of fork (0)
and "x" to the PARENT PROCESS is the actual system pid of the child process.*/
        
        if (x < 0) {  // just in case fork fails 
                puts("fork failure");
                exit(-1);
        }   
        else if (x == 0) { // therefore this block will be the child process 
                execlp("mpg123", "mpg123", "-q",command , 0); 
        }                   // see GNU docs, "system" also works                
        else {  printf("from parent: mpg123 is pid %d\nENTER to quit\n", x);
                sprintf(kil,"%s%d",kil,x);
                getchar();  // wait for user input
                system(kil);
                printf("All ");
        }       /* witness that the "else if" and "else" blocks are both executed here in parallel. The "else" 
   (parent) block is continuous with the rest of the program (since the PARENT PROCESS is actually
   the program itself) so... */
        printf("done.\n");
        exit(0);



I don't particularly understand how it works. I've replaced part of the execlp command with a variable 'command' (line 13), which is a char array.

If I specify 'command' to be a specific value e.g.

	//char selection;
	//selection = getchar();

	char command[5];
	//command[0]=selection;
	command[0]='2';
	command[1]='.';
	command[2]='m';
	command[3]='p';
	command[4]='3';
	command[5]='\0';



The .mp3 file is played fine no problems, however if I uncomment those lines not a thing (and obv. comment out assigning [0] to '2' . The tracks number 0-9.mp3, the intention is for the user to enter a number and the program will play the associated track.

It specifically stops working upon uncommenting the getch() line, even if I don't uncomment the assigning of command[0] to the value of selection, therefore I'm almost certain this is breaking it. Using cin and every other way of getting input I can think of produces the same result...I'm at a total loss here, if anyone could help I'd be v. grateful.

NB// I have it outputting the value stored in command to terminal and the output looks totally fine e.g. "7.mp3".

This post has been edited by Mr_GriM: 05 August 2011 - 10:14 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Char array with fixed value = fine, variable entry NO.

#2 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1002
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Char array with fixed value = fine, variable entry NO.

Posted 05 August 2011 - 10:24 AM

Can you define "not working"?

Using getchar it works fine (OSX).

EDIT: Just a note though, getchar() returns an int and selection should be typed as such.

This post has been edited by RudiVisser: 05 August 2011 - 10:25 AM

Was This Post Helpful? 0
  • +
  • -

#3 Mr_GriM  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-August 11

Re: Char array with fixed value = fine, variable entry NO.

Posted 05 August 2011 - 10:47 AM

I tried changing selection to type int and no joy there.

It compiles and spits out output to the effect of:

from parent: mpg123 is pid 6009
ENTER to quit
All done.



same as when the track is played, however the track is not played. To totally clarify not working, I mean that the .mp3 file is not played.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1