Click here to Skip to main content
15,843,623 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include <windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;
short ReadSect
       (const char *_dsk,    // disk to access
       char *&_buff,         // buffer where sector will be stored
       unsigned int _nsect   // sector number, starting with 0
       )
{
DWORD dwRead;   
HANDLE hDisk=CreateFile(_dsk,GENERIC_READ,0,0,OPEN_EXISTING,0,0);
if(hDisk==INVALID_HANDLE_VALUE) // this may happen if another program is already reading from disk
  {  
     CloseHandle(hDisk);
	 printf("Read  fail\n");
     return 1;
  }
SetFilePointer(hDisk,_nsect*512,0,FILE_BEGIN); // which sector to read
for (int f=0;f<512;f++)
printf("%c" ,_buff[0]);
printf("\n");

ReadFile(hDisk,_buff,512,&dwRead,0);  // read sector
CloseHandle(hDisk);
return 0;
}
short WriteSect
       (const char *_tdsk,    // disk to access
       char *&_buff,  // buffer where sector will be stored
       unsigned int _nsect   // sector number, starting with 0
       )
{
DWORD dwWrite;

HANDLE tDisk=CreateFile(_tdsk,GENERIC_WRITE,0,0,OPEN_EXISTING,0,0);
if(tDisk==INVALID_HANDLE_VALUE) // this may happen if another program is already reading from disk
  {  printf("write fail\n");
 GetLastError();

     CloseHandle(tDisk);
     return 1;
  }
SetFilePointer(tDisk,_nsect*512,0,FILE_BEGIN); // which sector to read
printf("%x\n",_buff[0]);

WriteFile(tDisk,_buff,512,&dwWrite,0);  // read sector
CloseHandle(tDisk);
printf("HURRRRRRRRRRRRReyyyyyyyy!!!!!!!!!\n");
return 0;
}

int main()
{
char * drv="\\\\.\\C:"; 
char *dsk="\\\\.\\PhysicalDrive2";
char *tdsk="\\\\.\\PhysicalDrive1";
int sector=5;




char *buffer=new char[512];

ReadSect(dsk,buffer,sector);
WriteSect(tdsk,buffer,sector);
printf("%d",sector);




getchar();

}


[EDIT: Code formatting; removed all caps from title]
Posted
Updated 1-Sep-13 1:41am
v2
Comments
Jochen Arndt 1-Sep-13 6:51am    
Where did the code fail and what is the error?

You should check every API call return value and get the error number with GetLastError() upon failure.
Angel-cpp 1-Sep-13 6:55am    
ACTUALLY ITS WRITE NOTHING ON THE GIVEN SECTOR
AND EXECUTES VERY NORMALLY
Jochen Arndt 1-Sep-13 7:02am    
Please do not write in all caps. The use of all caps is considered impolite, as it is perceived as "shouting".

How did you know that nothing is written? You code does not read the data after it has been written.

As noted before, you just must check the return values of the WriteFile(), ReadFile() and SetFilePointer() calls to know that there is no error.
Angel-cpp 9-Sep-13 5:47am    
yes this the solution
thanks
Angel-cpp 1-Sep-13 6:57am    
MY AIM IS TO COPY SECTOR 5 OF DISK1 TO SECTOR 5 OF DISK2 ..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900