《FloppyDisk_Write 的编写》

Linux 版本:

#include <unistd.h> #include <fcntl.h> #include <stdio.h>

int main(int argc, char *argv[])

{

  char boot_buf[512];   int fd_floppy, fd_src, result_write;  

  if(argc != 3)

  {     printf("error: parameters error !\n");     return -1;   }  

  if((fd_src = open(argv[1], O_RDONLY)) == -1)

  {    printf("error: open %s failed !\n", argv[1]);    return -1;   }

  read(fd_src, boot_buf, 512);

  if((fd_floppy = open(argv[2], O_RDWR)) == -1)  

  {    printf("error: open %s failed !\n", argv[2]);    exit(1);   }

    lseek(fd_floppy, 0, SEEK_SET);

 

  if((result_write = write(fd_floppy, boot_buf, 512)) == -1)

  {    printf("error: write %s failed !\n", argv[2]);    exit(1);   }  

  printf("Write data from %s to %s successfully !\n", argv[1], argv[2]);

  return 1;

}

Windows 版本:

#include <stdio.h> #include <stdlib.h>

#define BUF_SIZE 512

 

long FileSize(FILE *fp)  // it is use for get the file size;

{  

fseek(fp, 0L, SEEK_SET);  

fseek(fp, 0L, SEEK_END);  

long size = ftell(fp);

 fseek(fp, 0L, SEEK_SET);

 return size;

}

int main(int argc, char* argv[])

{  

FILE *fp_src, *fp_des;

 char buffer[BUF_SIZE];

 int count;

 if((fp_src = fopen(argv[1], "r")) == NULL)

 {   

printf("\topen %s failed !\n", argv[1]);   return 0;  

}

 if((fp_des = fopen(argv[2], "r+")) == NULL)

 {  

printf("\topen %s failed !\n", argv[2]);   

fclose(fp_src);   

return 0;  

}

 // Init_Img(fp_des);

 // read 512 bytes data from src_file ;

 count = fread(buffer, BUF_SIZE-2, 1, fp_src);  //once you use the file ok, you should close the file;

 fclose(fp_src);

 if(count != 1)  

{   

printf("\tread %s error !\n", argv[1]);   

return 0;  

}  // then set the buffer[510] buffer[511];

 buffer[510] = 0x55;

 buffer[511] = 0xAA;

 printf("the size of %s is : %ld\n", argv[1], FileSize(fp_src));  

printf("the size of %s is : %ld\n", argv[2], FileSize(fp_des));  // write 512 bytes data to des_file;  fseek(fp_des, 0, SEEK_CUR);  fflush(fp_des);

 count = fwrite(buffer, BUF_SIZE, 1, fp_des);

 printf("the size of %s is : %ld\n", argv[2], FileSize(fp_des));

 fclose(fp_des);  

 if(count != 1)

 {   

printf("\twrite %s error !\n", argv[2]);   

return 0;  

}

 printf("\twrite successfully !\n");

 return 1;

}

//  注意:Windows 下编写的这个程序比较苦逼;

// 你应当注意fopen 函数的相关用法,尤其是打开模式;((fp_des = fopen(argv[2], "r+"))  。。。mean that .......;

 

 

posted @ 2013-07-05 20:46  Auris  阅读(296)  评论(0编辑  收藏  举报