Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
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 ...
  1. #1
    Just Joined!
    Join Date
    Oct 2007
    Posts
    12

    Question 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!

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    1. For this to work, I assume you know that the destination drive must have at least as much data capacity as the source drive.
    2. For best results, make sure that both the source and destination drives are unmounted when you do this.
    3. If you wish to use the dd command, it's ideal for this. Do this at the command line:
      Code:
      man dd
      man system
      But rather than using your program to run plain and simple dd, do this at the command line:
      Code:
      which dd
      You'll get something like /usr/bin/dd. Use that instead of just saying dd. It makes for better security.
    4. 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.
    5. With every single call to a system function or a library function from a C or C++ program, do these things:
      1. Review the man page for parameter types and other requirements.
      2. In the man page, there are usually one or more #include statements in the synopsis. Use them.
      3. 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.

  3. #3
    Just 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?

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    so i can implement the use of 'dd' using c++ code?
    Yes, and post 2 of this thread explains how. Specific questions are welcome.

    can i just question what 'man' means?
    Sure. Go here.

  5. #5
    Just 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?

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    This is not so complicated as to require a tutorial.

    As stated in post 2 of this thread:
    Code:
    man system
    That shows you how to use shell commands inside a C++ program.

    Do you have questions about what's in that man page?

  7. #7
    Just 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!

  8. #8
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192

    A thousand apologies

    I think I misunderstood your question. I understood your question to be this:
    Given that I know C++, how do I use it to run dd?
    What I now believe your question to be is this:
    I don't 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.

    My new answer is that you should google
    Code:
    C++ tutorial
    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.

    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:
    Code:
    bash tutorial
    and knock yourself out.

    I hope this answer is more helpful.

  9. #9
    Just 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. #10
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    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.
    When does your professor or instructor say this task is due?

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...