Unix/Linux Go Back    


Linux RedHat, Ubuntu, SUSE, Fedora, Debian, Mandriva, Slackware, Gentoo linux, PCLinuxOS. All Linux questions here!

C++ Code to Access Linux Hard Disk Sectors (with a LoopBack Virtual Hard Disk)

Linux


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-26-2011   -   Original Discussion by shen747
shen747's Unix or Linux Image
shen747 shen747 is offline
Registered User
 
Join Date: Jan 2011
Last Activity: 29 January 2011, 4:05 AM EST
Posts: 12
Thanks: 8
Thanked 0 Times in 0 Posts
Unix or Linux Question C++ Code to Access Linux Hard Disk Sectors (with a LoopBack Virtual Hard Disk)

Hi all,

I'm kind of new to programming in Linux & c/c++. I'm currently writing a FileManager using Ubuntu Linux(10.10) for Learning Purposes. I've got started on this project by creating a loopback device to be used as my virtual hard disk. After creating the loop back hard disk and mounting it has the following configuration.


Code:
$> sudo fdisk -l /dev/loop0

Disk /dev/loop0: 10 MB, 10977280 bytes
255 heads, 63 sectors/track, 1 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/loop0 doesn't contain a valid partition table

Now what I want to do is develop a c++ program to read & write files to this loop back device,which I'm using to simulate an actual hard disk,at the blocks & sectors level. So far I've come up with the following code. But I'm still unable to read files from the hard disk one block at a time.


Code:
#include <iostream>
#include <stdio.h>

using namespace std;

int main()
{

    char block[512];
    int length=0;
    cout<<"Implementation of the File Handler Read Method..."<<endl;

    FILE *f = fopen("/dev/loop0", "r");
    if(f == NULL)
    {
        cout<<"Error In Opening the HardDisk File Retuning Error..."<<endl;
        return -1;
    }

    //Read One Block of Data to Buffer
    length = fread(block, 1, sizeof(block), f);

    /* Do something with the data */
    cout<<"Length : "<<length<<endl;

    return 0;
}


When I run this Program All what I get is the message for NULL.
"Error In Opening the HardDisk File Retuning Error...".
Could you please help me by pointing what am I doing wrong here ?. So I could open the loopback device as a file an access it at the sectors & block level.
Sponsored Links
    #2  
Old Unix and Linux 01-26-2011   -   Original Discussion by shen747
dsw's Unix or Linux Image
dsw dsw is offline
Registered User
 
Join Date: Aug 2010
Last Activity: 30 July 2014, 9:53 AM EDT
Location: Zürich
Posts: 46
Thanks: 2
Thanked 5 Times in 5 Posts
Check the perms on /dev/loop0


Code:
ls -l /dev/loop0

The Following User Says Thank You to dsw For This Useful Post:
shen747 (01-27-2011)
Sponsored Links
    #3  
Old Unix and Linux 01-26-2011   -   Original Discussion by shen747
shen747's Unix or Linux Image
shen747 shen747 is offline
Registered User
 
Join Date: Jan 2011
Last Activity: 29 January 2011, 4:05 AM EST
Posts: 12
Thanks: 8
Thanked 0 Times in 0 Posts
Unix or Linux Question

Quote:
Originally Posted by dsw View Post
Check the perms on /dev/loop0


Code:
ls -l /dev/loop0
HI dsw,

I changed the permissions of the /dev/loop0 to chmod 777 & then ran the command you gave and got the following results :


Code:
brw-rw---- 1 root disk 7, 0 2011-01-26 16:39 /dev/loop0

But now when I run the above Program I've written using the command prompt the following output appears :


Code:
Implementation of the File Handler Read Method...
Segmentation fault


Last edited by shen747; 01-26-2011 at 07:25 AM..
    #4  
Old Unix and Linux 01-26-2011   -   Original Discussion by shen747
dsw's Unix or Linux Image
dsw dsw is offline
Registered User
 
Join Date: Aug 2010
Last Activity: 30 July 2014, 9:53 AM EDT
Location: Zürich
Posts: 46
Thanks: 2
Thanked 5 Times in 5 Posts
Well it looks like you've got past the initial problem of the fopen() by changing perms.

I suspect you need to setup the fd first by doing an ftell() & fseek() as per the following example which should help you.

fread - C++ Reference

Failing that, add the -g flag to your g++ line and run it through gdb.
Sponsored Links
    #5  
Old Unix and Linux 01-26-2011   -   Original Discussion by shen747
shen747's Unix or Linux Image
shen747 shen747 is offline
Registered User
 
Join Date: Jan 2011
Last Activity: 29 January 2011, 4:05 AM EST
Posts: 12
Thanks: 8
Thanked 0 Times in 0 Posts
Hi dsw,

Thanks for the reply. I will check that.
Sponsored Links
    #6  
Old Unix and Linux 01-26-2011   -   Original Discussion by shen747
Corona688's Unix or Linux Image
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 14 December 2017, 4:58 PM EST
Location: Saskatchewan
Posts: 22,547
Thanks: 1,159
Thanked 4,287 Times in 3,955 Posts
Personally I'd use the open()/read()/write() calls instead of the stdio fopen()/fread()/fwrite() calls. You'll get more direct, unbuffered access; more useful error messages via perror() or errno; and a real file descriptor to call ioctl() on should you end up needing it. (which you might, dealing with block devices.)
Sponsored Links
    #7  
Old Unix and Linux 01-27-2011   -   Original Discussion by shen747
shen747's Unix or Linux Image
shen747 shen747 is offline
Registered User
 
Join Date: Jan 2011
Last Activity: 29 January 2011, 4:05 AM EST
Posts: 12
Thanks: 8
Thanked 0 Times in 0 Posts
Hi Corona688,

Could you please give me an example code sample using the above 03 methods. A pointer to get me started on them ?. Because I feel I'll have to deal with the file descriptor at some point & I feel it wouldn't be optimum to do so using the <stdio.h> methods. could you give me some help for me to get started with the : open()/read()/write() please ??.
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
declare disk driver for IDE hard disk ccc SCO 3 08-20-2010 03:22 PM
How to increase hard disk in linux pinga123 UNIX for Dummies Questions & Answers 7 05-31-2010 07:56 AM
hard disk and san lo-lp-kl AIX 4 06-12-2008 02:13 PM
Hard Disk hmaraver UNIX Desktop Questions & Answers 4 07-03-2005 04:50 PM
RAM, Hard Disk ramaraju UNIX for Dummies Questions & Answers 2 03-01-2005 08:35 PM



All times are GMT -4. The time now is 06:45 PM.