Advertisement
anhkiet2507

Cau2De2

Oct 6th, 2022
1,233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <dos.h>
  3. #include <memory.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. struct BOOT {  //for FAT16
  8.     char jmp[3];
  9.     char OEM[8];
  10.     int bytes_per_sector;
  11.     char sectors_per_cluster;
  12.     int reserved;
  13.     char FAT_cnt;
  14.     int ROOT_size;
  15.     int total_sectors;
  16.     char media;
  17.     int FAT_size;
  18.     int sectors_per_track;
  19.     int head_cnt;
  20.     long hidden_sectors;
  21.     long total_sectors_long;
  22.     char unknown[3];
  23.     long serial;
  24.     char volume[11];
  25.     char FAT_type[8];
  26.     char loader[448];
  27.     char mark[2];
  28. };
  29.  
  30. struct ROOT {
  31.     char name[8];
  32.     char ext[3];
  33.     char attr;
  34.     char reserved[10];
  35.     char time[2];
  36.     char date[2];
  37.     int first_cluster;
  38.     long size;
  39. };
  40.  
  41. void main()
  42. {
  43.     int i;
  44.     int drive = 3;
  45.     BOOT boot;
  46.  
  47.     int res = absread(drive, 1, 0, &boot);
  48.     unsigned int *fat = (unsigned int *)malloc (boot.FAT_size * boot.bytes_per_sector);
  49.  
  50.  
  51.     res = absread(drive, boot.FAT_size, boot.reserved, fat);
  52.  
  53.     unsigned int n = 5;
  54.     unsigned int cur = n;
  55.  
  56.     int num_byte = boot.ROOT_size * 32;
  57.  
  58.     ROOT *root = (ROOT *)malloc(num_byte);
  59.     if(root == NULL) return;
  60.  
  61.     int num_sector = num_byte / boot.bytes_per_sector;
  62.     int root_begin = boot.reserved + boot.FAT_size * boot.FAT_cnt;
  63.  
  64.     res = absread(drive, num_sector, root_begin, (void *)root);
  65.  
  66.  
  67.     printf("2. Tim 1 file co ten cho truoc va so thu tu cluster dau tien cua file do\n");
  68.     int k;
  69.     char str[9];
  70.     int first_cluster = -1;
  71.     for(i = 0; i < boot.ROOT_size; i++){
  72.         for(k = 0; k < 8 && root[i].name[k] != ' '; k++)
  73.             str[k] = root[i].name[k];
  74.         str[k] = 0;
  75.         char filename[8];
  76.         printf("\n Enter a file name:");
  77.         scanf("%s",filename);
  78.  
  79.         if(strcmp(str, filename) == 0){
  80.             first_cluster = root[i].first_cluster;
  81.             break;
  82.         }
  83.     }
  84.     printf("So thu tu cluster dau tien %d", first_cluster);
  85.    
  86.  
  87.  
  88.     free(root);
  89.     free(fat);
  90.  
  91.     getchar();
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement