(刚写的,有8.4G限制,谁有读取137G以上的函数,贴出来学习一下,据说是扩展13终端)
#include <bios.h>
#include <conio.h>
#include <stdio.h>
/*将读取的硬盘扇区以相应的扇区数为单位存到文件的函数(每次的读取数必须在65535字节内,否则无效)*/
void writeFile(unsigned char *buffer, unsigned int sector, char *filename)
{
FILE *f;
if((f=fopen(filename,"ab+"))==NULL)
{
printf("File Error\n");
return;
}
fwrite(buffer,512,sector,f);
fclose(f);
}
/*读取硬盘绝对扇区的函数(在8.4G内)*/
void readLBA(unsigned char hardDisk,
unsigned long startSector,
unsigned long nSector,
char *filename)
{
unsigned char head=0;
unsigned int cylinder=0;
unsigned char sector=0;
unsigned char pBuffer[32256]; /*63个扇区的字节数 */
FILE *f;
unsigned int allowednSector=0;
unsigned int doReadSector=0;
unsigned long sectorWrite=0;
unsigned int x=0,y=0;
/*获取当前光标 */
x=wherex();
y=wherey();
if((f=fopen(filename,"wb+"))==NULL)
{
printf("File Error\n");
return;
}
fclose(f);
/*把硬盘绝对扇区转换成CHS物理位置(柱面,磁头,扇区)*/
cylinder=startSector/(255*63);
head=(startSector/63)%255;
sector=startSector%63+1;
/*算出刚开始时可有效读取的最大扇区数 */
allowednSector=63-sector+1;
/*如果小的一次可以读完 */
if(nSector<=allowednSector)
doReadSector=nSector;
/*否则读可有效读取的最大扇区数 */
else doReadSector= allowednSector;
while(sectorWrite<nSector)
{
/*读到缓冲 */
biosdisk(2,hardDisk,head,cylinder,sector,doReadSector,pBuffer);
/*写到文件 */
writeFile(pBuffer,doReadSector,filename);
sectorWrite+= doReadSector;
/*显示已读的扇区数百分比 */
gotoxy(x,y);
cprintf("%lu%c sectors read.",sectorWrite*100/nSector,'%');
/*磁头归为0,准备每次读63扇区(一磁道的扇区数,也正好适合fwrite()函数的范围)*/
head++;sector=1;
if (head>254) {cylinder++; head=0; sector=1;}
if (cylinder>1023)
{printf("out of hard disk's cylinder range(0-1023)\n");
return;}
doReadSector=63;
/*快要读完了,不足63扇区了 */
if(nSector-sectorWrite<63) doReadSector=nSector-sectorWrite;
}
printf("\nDone.");
}
*****************************
/*以读取硬盘C盘 FAT32的FAT为例,来调用读取硬盘绝对扇区的函数 */
#include "readlba.h"
/*获取FAT参数(FAT前距离DBR的扇区数,FAT所占的扇区数)*/
void getFATinfo(unsigned int hardDisk,
unsigned int *reservedSector,
unsigned long *sectorPerFAT)
{
unsigned char buffer[512];
/*获取DBR,以便 获取FAT参数 */
biosdisk(2,hardDisk,1,0,1,1,buffer);
/* 获取FAT前距离DBR的扇区数 */
*reservedSector=*((unsigned int *)&buffer[0xE]);
/* 获取FAT所占的扇区数 */
*sectorPerFAT=*((unsigned long *)&buffer[0x24]);
}
void main()
{
unsigned int reservedSector;
unsigned long sectorPerFAT;
/* 获取FAT参数 */
getFATinfo(0x80,&reservedSector,§orPerFAT);
/*保存FAT到文件 */
readLBA(0x80,63+reservedSector,sectorPerFAT,"d:\\cfat.dat");
}
有缘相识! 我中文系的
半个月拿下基本C
一个月拿下基本C++
两个月拿下基本VC
一星期拿下一半基本JAVA
五天写DOS游戏
两个月写DOS游戏系统
年后一个月拿下VFP
过一下 ACCESS, SQL SERVER
与大家一起加油啊! 很喜欢这句
"拿下!"
http://mykool.go2.icpcn.com