View Full Version : how do i execute a command in C code?
twenglish1
November 28th, 2008, 03:36 PM
i am new to C programming but i am learning. i am trying to make a program that will format a drive but i don't know what code i would use to execute the format command.
here is the code:
#include "stdio.h"
int main(void){
char drive;
char x;
printf("Enter Drive letter:\n");
scanf("%c", &drive);
printf("Drive %c will now be formatted!\n", drive);
(format command goes here)
return 0;
}
what do i use to format the drive please help!!!
gray_hat
January 12th, 2009, 03:28 PM
A useful function for this is system(). The argument that this function takes is the command that you want executed, whatever you insert in the parentheses will be executed as if it is in a terminal/bash. For example:
int main(void) {
system("clear");
return 0;
}
will clear the screen anytime it is ran. So maybe something like:
#include "stdio.h"
int main(void){
char drive;
char x;
printf("Enter Drive letter:\n");
scanf("%c", &drive);
printf("Drive %c will now be formatted!\n", drive);
system("<command to format drive here>");
return 0;
}
Cuddles
January 12th, 2009, 03:31 PM
first, dont necropost, second, dont help morons on this site with trivial shit, they usually have bad intentions, like formatting their school computers......
ishkur88
January 12th, 2009, 04:22 PM
Plus, that won't work. You can't format a partition that you're using. Best you can hope to do is delete a bunch of shit and make the OS unstable.
And my God, you're using C.... why even try to make the call to system? There should be a fancier way of doing that.
twenglish1
January 12th, 2009, 04:39 PM
ok first of all im not trying to use it for a virus, i mean i know this is in hacking but its also under programming, i don't plan on formatting an active hard drive i know that doesn't work, i am making a program that sets up a usb drive for something, this includes formatting it and then copying the files to it. but whatever i'll try it
deathlord888
January 12th, 2009, 05:20 PM
lol you need to start with WAY WAY easier things than that...
Cuddles
January 12th, 2009, 05:23 PM
lol you need to start with WAY WAY easier things than that...
...whut are you talking about?
as to the question: if you want to set up a usb drive, what do you need to format it for? are you trying to make it bootable? are you trying to move--you know what, tell me the details of what you're trying to do, and ill try to help, asking random questions that come to mind isnt really gonna be of much help
twenglish1
January 12th, 2009, 05:36 PM
ok, i am trying to make a program that creates magic memory sticks for softmodding the psp, it was just out of being bored, cause i can easily make one with a command prompt, i just though it would be interesting to make a c program do it, and im still teaching myself c so i don't know everything about it
Cuddles
January 12th, 2009, 05:43 PM
i thought there were programs that did that already.....as to doing it in c, if you're gonna use system, you might as well just use a batch file, it'll be doing the same job, only you wont need to compile every time you change something....and it'll be a little easier to edit....but if you really want to do it in c, just make a batch file that does it, and for every line in the batch file, make a system call in your c programme's source containing the line.....if you dont want to do that, look into formatting drives in c online, google will be a better resource than asking on a forum, in this case
twenglish1
January 12th, 2009, 06:01 PM
i know i could use a batch file, thats what i did anyways, but i wanted to take it a step further and do it in c, and i searched google but didn't find much
ishkur88
January 12th, 2009, 06:29 PM
i know i could use a batch file, thats what i did anyways, but i wanted to take it a step further and do it in c, and i searched google but didn't find much
Doing it in Batch and doing it in C are almost complete polar opposite sides of the spectrum when you're talking about doing something like that.
Automating series of commands into a script (which is what you want to do) is in the realm of Batch. Doing that in C would require you to know a lot more, and doesn't really make sense.
Just KISS and use a Batch job to do your bidding.
*note to other users: This is one of the rare exceptions that I will endorse the use of a Batch file. He is simply wanting to make a trivial set of tasks easier... rather than trying to do childish bullshit to 'hack' the command prompt*
twenglish1
January 12th, 2009, 09:39 PM
ok, i wrote this code over with the system line in but it still won't work.
code:
#include "stdio.h"
char drive;
char x;
int main(void){
printf("Enter Drive letter:\n");
scanf("%c", &drive);
printf("Drive %c will now be formatted!\n", drive);
system("format &drive");
return 0;
}
it compiled but when i enter the drive letter nothing happens and it keeps repeating, its probably a really simple problem, but please give me a break, i'm fairly new to c programming
ishkur88
January 12th, 2009, 11:58 PM
Just at a glance, Its probably the drive letter itself that's the problem.
I think the syntax for the 'format' command is as follows:
format C:\
If I'm not mistaken.
In your program, you're storing the drive letter in a single char, instead of a char array or a string. This means that 'format' will, at best, get "C" as its argument (instead of "C:\")... which it may or may not be able to accept.
Take a look at this (http://gd.tuwien.ac.at/languages/c/programming-bbrown/c_055.htm) for some help with character arrays (strings) and how to reading user input.
And... again, I don't think a call to system is the best way to accomplish this task. All you're making is a overzealous batch file. Do you want to format the partition, or delete all files?
If you just want to delete the files, you can take a look at what they've done here:
http://www.java2s.com/Code/C/File/Deleteafile.htm
twenglish1
January 13th, 2009, 07:32 AM
yah i know that is the syntax but my problem is getting the char with the drive letter into the command
Truant
June 23rd, 2009, 11:20 PM
i am new to C programming but i am learning. i am trying to make a program that will format a drive but i don't know what code i would use to execute the format command.
here is the code:
#include "stdio.h"
int main(void){
char drive;
char x;
printf("Enter Drive letter:\n");
scanf("%c", &drive);
printf("Drive %c will now be formatted!\n", drive);
(format command goes here)
return 0;
}
what do i use to format the drive please help!!!
Here is the idea: do what you did, strncat the drive letter, the command, and the semicolon, and then call system() and pass the array.
#include "stdio.h"
int main(void){
char drive;
char x;
char cmd [ 20 ];
printf("Enter Drive letter:\n");
scanf("%c", &drive);
printf("Drive %c will now be formatted!\n", drive);
cmd = strncat ( "format ", drive );
cmd = strncat ( cmd, ":" );
system ( cmd );
return 0;
}
This code will only work for Windows, and I am not sure that you use strncat. It maybe different.
DoG MikkyW
June 24th, 2009, 02:10 AM
Sweet!!! This is the first necro-post that can't be counted as a necro-post because it actually adds something important to the main subject. Kudos.
putkapopper1990
June 24th, 2009, 01:07 PM
not quite sure but my friend can do all this real good,.
Truant
June 30th, 2009, 05:41 PM
So ask him, and have something to add to the conversation before posting! Spam posts aren't good.
gray_hat
July 6th, 2009, 11:58 AM
ok, i wrote this code over with the system line in but it still won't work.
code:
#include "stdio.h"
char drive;
char x;
int main(void){
printf("Enter Drive letter:\n");
scanf("%c", &drive);
printf("Drive %c will now be formatted!\n", drive);
system("format &drive");
return 0;
}
it compiled but when i enter the drive letter nothing happens and it keeps repeating, its probably a really simple problem, but please give me a break, i'm fairly new to c programming
Instead of system("format &drive"); (that doesn't even work) do:
char formatbuf[20];
scanf("%c",&drive);
snprintf(formatbuf, sizeof(drivebuf),"format %c\\:",drive)
system(formatbuf);
i'm not sure if this information is needed anymore or not and i'm sure someone will destructively criticize it in someway but w/e. i don't even care at all.
twenglish1
July 6th, 2009, 09:46 PM
thanks gor the info i didn't need it any more but i appreciate you taking the time to answer
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.