• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

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(each 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);
0
wenfeng
Asked:
wenfeng
  • 2
1 Solution
 
wenfengAuthor Commented:
Edited text of question.
0
 
imladrisCommented:
I am not entirely clear on everything that is going on, though the overall idea seems allright.
For instance in this line:

status = biosdisk(READ, drive, head, track, 1, spt, bufbase);

What is bufbase?
And at the end:

bytesWritten += write(fin, data[datanum], buflength*3);

What does write do? Comment says write to a file, but top comment indicates you are doing diskcopy.

Anyway, this line:

//now write the 20*2 tracks of data to the buffer
         
strncpy(data[j/3] + buflength*(j%3), bufbase, buflength);

might have a problem. From the comment and the statement it appears you are attempting to copy the information read from the disk to the data buffer. However, strncpy will stop copying if it encounters a 0.
0
 
deightonCommented:
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'.
0
 
wenfengAuthor Commented:
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
0

Featured Post

Managing Security & Risk at the Speed of Business

Gartner Research VP, Neil McDonald & AlgoSec CTO, Prof. Avishai Wool, discuss the business-driven approach to automated security policy management, its benefits and how to align security policy management with business processes to address today's security challenges.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now