教程中国
PHOTOSHOP CS9.0中文版 MAYA 8.5 FOR WINDOWS Corel Painter v9.0 Flash MX2004 中文版 Illustrator cs2 中文版
VC++6.0含sp6 中英文版 VB6.0 +sp6 简体中文版 Borland Delphi 7汉化版 MSDN for vb6.0中文版 Visual Studio 2005简体
教程中国下属 文件存储共享专家BIBIDU.COM 提供大型软件,教材,源码,电影,音乐,图书等下载 更多精品请点此进入
  您目前所在位置: 教程中国 >> C >> 编程实例 >> 用C读写原始分区 RSS订阅
用C读写原始分区
教程(视频,书籍)下载:  ASP.NET AutoCAD 数据库 C# ASP java photoshop 网页设计 delphi 3dmax Flash C++ VB 张孝祥 实例   更多请进入BIBIDU搜索
IT搜索引擎   

一篇转贴的邮件列表的内容,关于读写原始分区的知识
想要变态地榨取原始设备的空间和速度,就用他吧

Content-Type: multipart/mixed;
boundary="------------D4869FCB9AEAF2CC69FB9DEF"


This is a multi-part message in MIME format.
--------------D4869FCB9AEAF2CC69FB9DEF
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit


"Mayank Vasa" <mvasa@confluencenetworks.com> wrote:
> I am quite new to rawio and am experimenting with with its usage. My test
> environment is Redhat 7.0, kernel version 2.2.16-22 having an external fibre
> channel drive having 2 disks (/dev/sda1 and /dev/sdb1)
>
> All I am trying to do is to write and read to & from the disk using a raw
> device. Externally I did a "raw /dev/raw/raw1 /dev/sdb1" and then I wrote a
> small program to do the read/write.


[snip]


Raw devices need to meet the alignment requirements of the
device they are bound to; in the case of most disk this
will be 512 bytes. You need to take this into account for:
- the buffer you give to the read() and write() calls
- the 'size' given to read() and write() should be a
multiple of 512
- the SEEK_SET 'offset' given to lseek() should be a
multiple of 512. Note you have a 2 G limit here.
You can use _llseek() to get around this.


A small program that just reads from a raw device (or the
corresponding block device which should give the same
result) attached.


If you were binding a raw device to a cdrom device then
the BLKSIZE would need to be 2048 bytes (in most cases).


Doug Gilbert
--------------D4869FCB9AEAF2CC69FB9DEF
Content-Type: text/plain; charset=us-ascii;
name="my_rawio_ex.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="my_rawio_ex.c"


#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>


#define BLKSIZE 512
#define BLKS2READ 1


int main(int argc, char *argv[])
{
int fd, k;
unsigned char buff[BLKSIZE * (BLKS2READ + 1)]; // allow extra for alignment
unsigned char * arbp; // aligned read buffer ptr
long block_addr = 0;


arbp = (char *)(((unsigned long)buff + (BLKSIZE - 1)) & (~(BLKSIZE - 1)));


fd = open(argv[1], O_RDONLY);
if (fd < 0) {
perror("open");
exit (1);
}
if ((lseek(fd, block_addr * BLKSIZE, SEEK_SET)) < 0){
perror("lseek"); // problem if 2nd arg > 2G
exit (1);
}
if ((read(fd, arbp, BLKSIZE * BLKS2READ)) < 0) {
perror("read");
exit(1);
}


printf("First 16 bytes of the readbuf (in hex) are:\n ");
for (k = 0; k < 16; ++k)
printf("%x ", (int)arbp[k]);
printf("\n");
close(fd);
return 0;
}

来源:十度教育
作者:
关键字:用C读写原始分区
发表日期:2006-8-12 23:08:34

网页显示有限 阅读全文请下载本文完整版WORD文档

上一篇:一个图书馆管理程序   下一篇:非常漂亮的4瓣花型图案


2008-11-23 21:06:00
本文的相类似文章
在学习中进步 在进步中成长 教程中国相随您的成长之路
华腾联合科技股份有限公司版权所有
广告联系:Rosibo@163.com