wenfeng
asked on
What's wrong with my codes?
Hello,
I want to develop a diskcopy. I allocate 14 32K, in momory, to contain the 20 tracks, which needs ( 2(sides)*20(tracks)*512(ea ch sectors)*18(sectors each track). Each track is 512*18 = 9216(bytes). So every 32K can only contain 3 tracks. To contain 40 tracks(20*2), I need 14 32K ( the first 13 contains 13*3(each contains 3 track, plus the last 32K contains only one track, 13*3 + 1 = 40).
Than's my idea. (If silly, do tell me).
The following is my codes. I will get a image file. when I write this image file back to diskette using rawrite2.exe, the contents is no longer the original.
where could it be wrong? (or my algorithm is wrong at all).
/*======================== ========== ========== ========== ======*
* Allocate 14 32K to contain the 20 track (20*2*18*512) *
*========================= ========== ========== ========== =====*/
for ( i = 0; i<14; i++)
{
segsize = SEGMENTSIZE;
data[i] = (char far*) _fmalloc(segsize);
if( data[i] == NULL)
{
printf("Not enough memory left, 360K needed");
exit(0);
}
}
/*======================== ========== ========== ========== ======*
* Copy data, each time 20 tracks *
*========================= ========== ========== ========== =====*/
for (i = 0; i< 4; i++)
{
head = track = 0;
/*======================== ========== ========== ========== ======*
* Read the 20 tracks *
*========================= ========== ========== ========== =====*/
puts("Pls insert the SOURCE diskette");
// getch();
track = i*20;
j = 0;
while ( track < (i+1)*20 )
{
status = biosdisk(READ, drive, head, track, 1, spt, bufbase);
if (status != 0)
{
printf("Error take place\n");
Error(status);
}
//now write the 20*2 tracks of data to the buffer
strncpy(data[j/3] + buflength*(j%3), bufbase, buflength);
printf("strncpy to %d, %d \n", j/3, j%3);
j++;
if ((head = (head + 1) & 1) == 0)
{
++track;
}
}
/* the above wring to buffer is finished, now write this buffer to a file*/
for( datanum = 0; datanum < 13; datanum++ )
{
bytesWritten += write(fin, data[datanum], buflength*3);
}
bytesWritten += write(fin, data[datanum], buflength*1);
printf("Bytes written %ld\n", bytesWritten);
I want to develop a diskcopy. I allocate 14 32K, in momory, to contain the 20 tracks, which needs ( 2(sides)*20(tracks)*512(ea
Than's my idea. (If silly, do tell me).
The following is my codes. I will get a image file. when I write this image file back to diskette using rawrite2.exe, the contents is no longer the original.
where could it be wrong? (or my algorithm is wrong at all).
/*========================
* Allocate 14 32K to contain the 20 track (20*2*18*512) *
*=========================
for ( i = 0; i<14; i++)
{
segsize = SEGMENTSIZE;
data[i] = (char far*) _fmalloc(segsize);
if( data[i] == NULL)
{
printf("Not enough memory left, 360K needed");
exit(0);
}
}
/*========================
* Copy data, each time 20 tracks *
*=========================
for (i = 0; i< 4; i++)
{
head = track = 0;
/*========================
* Read the 20 tracks *
*=========================
puts("Pls insert the SOURCE diskette");
// getch();
track = i*20;
j = 0;
while ( track < (i+1)*20 )
{
status = biosdisk(READ, drive, head, track, 1, spt, bufbase);
if (status != 0)
{
printf("Error take place\n");
Error(status);
}
//now write the 20*2 tracks of data to the buffer
strncpy(data[j/3] + buflength*(j%3), bufbase, buflength);
printf("strncpy to %d, %d \n", j/3, j%3);
j++;
if ((head = (head + 1) & 1) == 0)
{
++track;
}
}
/* the above wring to buffer is finished, now write this buffer to a file*/
for( datanum = 0; datanum < 13; datanum++ )
{
bytesWritten += write(fin, data[datanum], buflength*3);
}
bytesWritten += write(fin, data[datanum], buflength*1);
printf("Bytes written %ld\n", bytesWritten);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Re - the comment about strncpy, you can hold you data in arrays of characters (as bytes) but you can't use functions that use the NULL terminated string ('cos 0 might be anywhere)
You can use instead of strncpy
memcpy
#include <string.h>
void *memcpy(void *buf1, void *buf2, int count);
Copy `count' bytes from buffer `buf2' to buffer `buf1'.
Returns `buf1'.
You can use instead of strncpy
memcpy
#include <string.h>
void *memcpy(void *buf1, void *buf2, int count);
Copy `count' bytes from buffer `buf2' to buffer `buf1'.
Returns `buf1'.
ASKER
You are right.
when I replace the diskette, I can't an error messasge, "Floppy disk changed, what should I do to prevent such things",
Thanks
when I replace the diskette, I can't an error messasge, "Floppy disk changed, what should I do to prevent such things",
Thanks
ASKER