User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 423,654 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,095 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 337 | Replies: 5 | Solved
Reply
Join Date: Aug 2008
Posts: 7
Reputation: soppyhankins is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
soppyhankins's Avatar
soppyhankins soppyhankins is offline Offline
Newbie Poster

Help with variables and filenames

  #1  
Aug 6th, 2008
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,126
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 929
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Help with variables and filenames

  #2  
Aug 6th, 2008
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.
Last edited by Ancient Dragon : Aug 6th, 2008 at 11:42 pm.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Apr 2004
Posts: 3,615
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 142
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Help with variables and filenames

  #3  
Aug 6th, 2008
Originally Posted by Ancient Dragon View Post
// 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.
Reply With Quote  
Join Date: Dec 2005
Posts: 3,603
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 22
Solved Threads: 414
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: Help with variables and filenames

  #4  
Aug 7th, 2008
> 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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Reply With Quote  
Join Date: Aug 2008
Posts: 8
Reputation: xitrum69 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 2
xitrum69 xitrum69 is offline Offline
Newbie Poster

Re: Help with variables and filenames

  #5  
Aug 7th, 2008
I suppose that you are using C language. Here is your function

  1. #include <stdlib.h>
  2.  
  3. int RunSystemCmd(char *pfilename)
  4. {
  5. char buff[512] = {0};
  6. if (pfilename == NULL) return -1;
  7. 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);
  8. return system(buff);
  9. }
  10.  
Reply With Quote  
Join Date: Aug 2008
Posts: 7
Reputation: soppyhankins is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
soppyhankins's Avatar
soppyhankins soppyhankins is offline Offline
Newbie Poster

Re: Help with variables and filenames

  #6  
Aug 7th, 2008
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!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 10:50 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC