Find the answer to your Linux question:
Results 1 to 2 of 2
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 ...
  1. #1
    Just Joined!
    Join Date
    Jan 2011
    Posts
    1

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

    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.

  2. #2
    Just Joined!
    Join Date
    Jun 2010
    Location
    Darien, IL
    Posts
    36
    Did you try running it as root? Sudo something? I'm not sure how to do it in Ubuntu. Someone else may chime in.

Posting Permissions

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