Results 1 to 10 of 11
Hi, I'm very new to Linux so i apologies now if my question is dumb but hopefully you guys can help me out here. I'm trying to create a program ...
- 10-28-2007 #1Just Joined!
- Join Date
- Oct 2007
- Posts
- 12
using linux commands in my c++ program?
Hi, I'm very new to Linux so i apologies now if my question is dumb but hopefully you guys can help me out here. I'm trying to create a program in c++ that will create and exact image of a thumb drive. After doing research i have come across a 'dd' image command? This to me sounds like what i need to create a copy but is it possible to implement these commands in my c++ program?
If so could anyone point me in the direction of books/resources where i could get extra information?
Any input will be GREATLY appreciated!
- 10-28-2007 #2
- For this to work, I assume you know that the destination drive must have at least as much data capacity as the source drive.
- For best results, make sure that both the source and destination drives are unmounted when you do this.
- If you wish to use the dd command, it's ideal for this. Do this at the command line:
But rather than using your program to run plain and simple dd, do this at the command line:Code:man dd man system
You'll get something like /usr/bin/dd. Use that instead of just saying dd. It makes for better security.Code:which dd
- If you wish to be uebercool, don't use dd. Use open(), read(), write(), and close. If you wish to be clever, also use lseek() in a carefully designed loop to find out quickly the data capacity of your drive, so you can display a progress indication.
- With every single call to a system function or a library function from a C or C++ program, do these things:
- Review the man page for parameter types and other requirements.
- In the man page, there are usually one or more #include statements in the synopsis. Use them.
- For every library call or system call which returns a status (most of them do), be sure to check the return value of the function to determine whether an error occurred. If you don't have this habit, you will someday spend hours and hours trying to track down why something misbehaves in some subtle way.
Hope this helps.
- 10-28-2007 #3Just Joined!
- Join Date
- Oct 2007
- Posts
- 12
thanks im starting to get somewhere here now,
so i can implement the use of 'dd' using c++ code?
can i just question what 'man' means?
- 10-28-2007 #4
- 10-28-2007 #5Just Joined!
- Join Date
- Oct 2007
- Posts
- 12
ok im going to ask som more 'stupid' questions i think
Do you know of any tutorials that are available for using linux commands inside the c++ environment because this is a big part of where im struggling. I just don't understand a way of using the 'dd' image function when it is unix language. I would have thought my C++ compiler will just not recognize the syntax?
- 10-28-2007 #6
This is not so complicated as to require a tutorial.
As stated in post 2 of this thread:
That shows you how to use shell commands inside a C++ program.Code:man system
Do you have questions about what's in that man page?
- 10-28-2007 #7Just Joined!
- Join Date
- Oct 2007
- Posts
- 12
sorry i guess im just not understanding because im new to all of this
'man system' - what do i do with this? how do i embed this in my c++ code so that i can use it to access my device? I guess im just not getting how to incorporate the UNIX commands within the c++ code
my apologies for asking so many question and thanks for answering them!
- 10-28-2007 #8
A thousand apologies
I think I misunderstood your question. I understood your question to be this:
What I now believe your question to be is this:Given that I know C++, how do I use it to run dd?
That's an entirely different question, and I can understand your frustration at my answers.I don't know C++. How do I use it to run dd?
My new answer is that you should google
and pick a tutorial that works for you. As you work along in a C++ tutorial, keep an eye on the answers that I've already given in this thread. At some point they will begin to make sense.Code:C++ tutorial
You may also come to ask yourself, "Why do I have to learn all this just to run dd?"
The answer is: you don't. There are far more straightforward ways to use dd. Put the command in a shell script, to begin with. For more information on writing shell scripts in particular and using bash (which is the shell you're using, most likely) in general, google this:
and knock yourself out.Code:bash tutorial
I hope this answer is more helpful.
- 10-28-2007 #9Just Joined!
- Join Date
- Oct 2007
- Posts
- 12
thanks for your patients.
Basically im new to c++, i prefer java but hey.
My task is that i have to produce some program that will allow me to create an exact image of the contents of a thumb drive. Personally i'm not fussy in which ever way i do it or whatever language but i seemed to get drawn down this path. I think i get the idea of how this could work, and i believe i need something like follows
dd if=\\?\Device\Harddisk1\Partition0 of=c:\temp\usb2.img bs=1M --size --progress
am i being stupid or is what im trying to achieve not as hard as im thinking? is it a case of just working out how to place the above code and odds and ends into my c++ program
Ps. VERY greatful for all the help
- 10-28-2007 #10When does your professor or instructor say this task is due?My task is that i have to produce some program that will allow me to create an exact image of the contents of a thumb drive.


Reply With Quote