• C

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);
wenfengAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
deightonprogCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.