how to write data into disk physical memory directly using C in windows

Status
Not open for further replies.

Venkadesh_M

Advanced Member level 4
Joined
Jun 26, 2013
Messages
1,374
Helped
258
Reputation
516
Reaction score
254
Trophy points
1,363
Location
Coimbatore, India
Activity points
8,019
I want to directly access the physical sectors of a memory card using C in windows. How can i do this? Also i dont worry about the file system. I want to make the card compatible with my micro controller..

If this post is not relevent to this topic, moderators please do consider.

Thanks in advance..!
 

I got a way to do that,
Code C - [expand]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#pragma pack( 1 )

struct boot
{
BYTE jump [ 3 ] ;
char bsOemName [ 8 ] ;
WORD bytesperSector ;
BYTE sectorspercluster ;
WORD sectorsreservedarea ;
BYTE copiesFAT ;
WORD maxrootdirentries ;
WORD totalSectors ;
BYTE mediaDescriptor ;
WORD sectorsperFAT ;
WORD sectorsperTrack ;
WORD sides ;
WORD hiddenSectors ;
char reserve [ 480 ] ;
} ;

void ReadSector ( char*src, int ss, int num, void* buff ) ;

void display_buff(char *);

**** unsigned char *ptr;
****
**** int i, j, k;
****
**** struct boot b ;


void main()
{ ****
****ReadSector ( "\\\\.\\F:", 0, 1, &b ) ;
****
****printf ( "Boot Sector name: %s\n", b.bsOemName ) ;
****printf ( "Bytes per Sector: %d\n", b.bytesperSector ) ;
****printf ( "Sectors per Cluster: %d\n", b.sectorspercluster ) ;
****printf ( "jump : %c%c%c\n",************b.jump[0], b.jump[1], b.jump[2] );
****printf ( "sectorsreservedarea: %d\n", ****b.sectorsreservedarea) ;
****printf ( "copiesFAT: %d\n", ************b.copiesFAT) ;
****printf ( "maxrootdirentries: %d\n", ****b.maxrootdirentries) ;
****printf ( "totalSectors: %d\n", ************b.totalSectors) ;
****printf ( "mediaDescriptor: %d\n", ********b.mediaDescriptor) ;
****printf ( "sectorsperFAT: %d\n", ********b.sectorsperFAT) ;
****printf ( "sectorsperTrack: %d\n", ********b.sectorsperTrack) ;
****printf ( "sides: %d\n", ****************b.sides) ;
****printf ( "hiddenSectors: %d\n", ********b.hiddenSectors) ;

****display_buff(&b);

****for(k = 0; k < 10; k++)
****{****
********ReadSector ( "\\\\.\\F:", k, 1, &b );
********display_buff(&b);
****}

}


void ReadSector ( char *src, int ss, int num, void* buff )
{
****HANDLE h ;
****unsigned int br ;
****h = CreateFile ( src, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0 ) ;
****printf("\nsource name: %s\n", src);
****printf("file handler: %d\n", h);
****SetFilePointer ( h, ( ss * 512 ), NULL, 0 ) ;
****ReadFile ( h, buff, 512 * num, &br, NULL );
****CloseHandle ( h ) ;
}

void display_buff(char *buff)
{
****ptr = buff;
****
****for(j = 0; (j*16+i) < 512; j++)
****{
********printf("\n");
********for(i = 0; i < 16 && (j*16+i) < 512 ; i++)****
********printf("%.2X ", *ptr++);
********printf(" ");
********ptr -= i;
********for(i = 0; i < 16 && (j*16+i) < 512 ; i++)****
********if(*ptr >= ' ')
********printf("%c", *ptr++);
********else
********printf(".", *ptr++);
****}

}



This is an example code to demonstrate the Disk read write in windows compiled using Dev C++, but it will only work for removable drives in 8.1..

'F:' is the disk name of the removable drive.


Code C - [expand]
1
void ReadSector ( Disk name, sector number, number of sectors, Read Buff address ) ;



Interested people can have a try on this...
 
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…