I am trying to create a simple C program that uses HandBrake (video converter). The only way to run this program is to have one of the (in my opinion) crappy GUIs or to type out all of the arguments in the command line. My program is simpler than the GUIs, and I don't need to type all of the arguments. I have everything ready except for one flaw. My program takes input that you want to be the file name after it is done converting. It saves the input in a variable named "filename". I then use system to run HandBrake and all of it's arguments. The code looks like this:
system("HandBrakeCLI -i /dev/hdc -o /root/??? -b 1024 -B 128 -R 48 -E faac -f mp4 -w 368 -l 208 -m");
Of course, the question marks aren't supposed to be there. That's where I'm stuck. I would like to have the input from the "filename" variable to be placed where the question marks are. I cannot use "$filename" because it shows an error while compiling. It might be something small that I missed, or I might just be doing it wrong. Any help is appreciated!

soppyhankins

declare a character array that is large enough to contain the filename and all the arguments, then pass that to the system() function. Example:

char command[512] = {0};
char filename[255];
printf("Enter a file name\n");
fgets(filename,sizeof(filename),stdin);
// remove '\n' 
filename[strlen(filename)-1] = 0;
sprintf(command,"HandBrakeCLI -i /dev/hdc -o /root/%s -b 1024 -B 128 -R 48 -E faac -f mp4 -w 368 -l 208 -m", filename);
system(command);

You may have to adjust the above if filename contains spaces.

// remove '\n' 
filename[strlen(filename)-1] = 0;

It is wise to check to see whether or not the last character is actually a '\n' before destroying it.

Comments
Agree :)
Not only wise but essential, I would say.

> I am trying to create a simple C program that uses HandBrake (video converter).
Is this a deliberate exercise in using C, or are you really just after a quick way of solving the problem, and you thought C might be the best answer?

Because if you're willing to entertain alternatives, then consider using the shell. Especially if all you're doing in your C program is a bit of string manipulation then calling system().

#!/bin/bash
echo -n "What filename > "
read filename
HandBrakeCLI -i /dev/hdc -o /root/$filename -b 1024 -B 128 -R 48 -E faac -f mp4 -w 368 -l 208 -m

Note: the last line is all one line, ignore the forum line wrapping in code tags.

I suppose that you are using C language. Here is your function

#include <stdlib.h>

int  RunSystemCmd(char *pfilename)
{
    char buff[512] = {0};
    if (pfilename == NULL) return -1;
    sprintf(buff,"HandBrakeCLI -i /dev/hdc -o /root/%s -b 1024 -B 128 -R 48 -E faac -f mp4 -w 368 -l 208 -m", pfilename);
    return system(buff);
}

Thank you all for your help! I did make this in C because I wanted to practice some more on I/O and variables. I got it working thanks to all of you!

This question has already been answered. Start a new discussion instead.