0% found this document useful (0 votes)
8 views6 pages

HDH

The document contains multiple C++ programs that interact with the file system, specifically reading the boot sector and directory entries of a drive. Each program demonstrates different functionalities such as reading the boot sector, listing directory contents, finding file entries, and counting empty clusters. The code utilizes structures to represent boot sector and directory entry data, and handles binary file operations.

Uploaded by

vndt181204
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

HDH

The document contains multiple C++ programs that interact with the file system, specifically reading the boot sector and directory entries of a drive. Each program demonstrates different functionalities such as reading the boot sector, listing directory contents, finding file entries, and counting empty clusters. The code utilizes structures to represent boot sector and directory entry data, and handles binary file operations.

Uploaded by

vndt181204
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Bài 1

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdint>

using namespace std;


struct BootSector {
uint8_t jumpCode[3];
char oemName[8];
uint16_t bytesPerSector;
uint8_t sectorsPerCluster;
uint16_t reservedSectors;
uint8_t numberOfFATs;
uint16_t rootEntries;
uint16_t totalSectors;
uint8_t mediaDescriptor;
uint16_t sectorsPerFAT;
uint16_t sectorsPerTrack;
uint16_t numberOfHeads;
uint32_t hiddenSectors;
uint32_t largeTotalSectors;
};

int main() {

const char* drivePath = "\\.\C:";

ifstream drive(drivePath, std::ios::binary);


if (!drive.is_open()) {
cerr << "Loi khong mo duoc o dia" << std::endl;
return 1;
}

BootSector bs;
drive.read(reinterpret_cast<char*>(&bs), sizeof(BootSector));
drive.close();

cout << "OEM Name: " << std::string(bs.oemName, 8) << endl;


cout << "Bytes per Sector: " << bs.bytesPerSector << endl;
cout << "Sectors per Cluster: " << static_cast<int>(bs.sectorsPerCluster) <<endl;
cout << "Reserved Sectors: " << bs.reservedSectors << endl;
cout << "Number of FATs: " << static_cast<int>(bs.numberOfFATs) << endl;
cout << "Root Entries: " << bs.rootEntries << endl;
cout << "Total Sectors: " << bs.totalSectors << endl;
cout << "Media Descriptor: " << static_cast<int>(bs.mediaDescriptor) << endl;
cout << "Sectors per FAT: " << bs.sectorsPerFAT << endl;
cout << "Sectors per Track: " << bs.sectorsPerTrack << endl;
cout << "Number of Heads: " << bs.numberOfHeads << endl;
cout << "Hidden Sectors: " << bs.hiddenSectors << endl;
cout << "Large Total Sectors: " << bs.largeTotalSectors << endl;

return 0;
}
Bài 2
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdint>

using namespace std;


struct BootSector {
uint8_t jumpCode[3];
char oemName[8];
uint16_t bytesPerSector;
uint8_t sectorsPerCluster;
uint16_t reservedSectors;
uint8_t numberOfFATs;
uint16_t rootEntries;
uint16_t totalSectors;
uint8_t mediaDescriptor;
uint16_t sectorsPerFAT;
uint16_t sectorsPerTrack;
uint16_t numberOfHeads;
uint32_t hiddenSectors;
uint32_t largeTotalSectors;
};

int main() {

const char* drivePath = "\\.\C:";

ifstream drive(drivePath, std::ios::binary);


if (!drive.is_open()) {
cerr << "Loi khong mo duoc o dia" << std::endl;
return 1;
}

BootSector bs;
drive.read(reinterpret_cast<char*>(&bs), sizeof(BootSector));
drive.close();

cout << "OEM Name: " << std::string(bs.oemName, 8) << endl;


cout << "Bytes per Sector: " << bs.bytesPerSector << endl;
cout << "Sectors per Cluster: " << static_cast<int>(bs.sectorsPerCluster) <<endl;
cout << "Reserved Sectors: " << bs.reservedSectors << endl;
cout << "Number of FATs: " << static_cast<int>(bs.numberOfFATs) << endl;
cout << "Root Entries: " << bs.rootEntries << endl;
cout << "Total Sectors: " << bs.totalSectors << endl;
cout << "Media Descriptor: " << static_cast<int>(bs.mediaDescriptor) << endl;
cout << "Sectors per FAT: " << bs.sectorsPerFAT << endl;
cout << "Sectors per Track: " << bs.sectorsPerTrack << endl;
cout << "Number of Heads: " << bs.numberOfHeads << endl;
cout << "Hidden Sectors: " << bs.hiddenSectors << endl;
cout << "Large Total Sectors: " << bs.largeTotalSectors << endl;

return 0;
}
Bài 3
#include <iostream>
#include <dirent.h>

using namespace std;

int main() {
cout<<"Nhap vao duong dan thu muc:"<<endl;
string path ;
getline(cin,path);
DIR* dir;
struct dirent* ent;

dir = opendir(path.c_str());
if (dir != nullptr) {
while ((ent = readdir(dir)) != nullptr) {
cout << ent->d_name << endl;
}
closedir(dir);
} else {
cerr << "khong the mo thu muc" << endl;
return 1;
}

return 0;
}

Bài 4
#include <iostream>
#include <fstream>
#include <cstdint>
#include <string>
#include <vector>
#include <stdexcept>

using namespace std;

struct BS {
uint8_t jmp[3];
char oem[8];
uint16_t bps;
uint8_t spc;
uint16_t rsvdSecCnt;
uint8_t numFATs;
uint16_t rootEntCnt;
uint16_t totSec16;
uint8_t media;
uint16_t secPerFAT;
uint16_t secPerTrk;
uint16_t numHeads;
uint32_t hiddSec;
uint32_t totSec32;
};

struct DirEntry {
char name[11];
uint8_t attr;
uint8_t ntRes;
uint8_t crtTimeTenth;
uint16_t crtTime;
uint16_t crtDate;
uint16_t lstAccDate;
uint16_t fstClusHI;
uint16_t wrtTime;
uint16_t wrtDate;
uint16_t fstClusLO;
uint32_t fileSize;
};

BS readBS(ifstream& drv) {
BS bs;
drv.read(reinterpret_cast<char*>(&bs), sizeof(BS));
return bs;
}

vector<uint16_t> readFAT(ifstream& drv, const BS& bs) {


int fatStart = bs.rsvdSecCnt * bs.bps;
vector<uint16_t> fat(bs.secPerFAT * bs.bps / sizeof(uint16_t));
drv.seekg(fatStart, drv.beg);
drv.read(reinterpret_cast<char*>(fat.data()), fat.size() * sizeof(uint16_t));
return fat;
}

DirEntry findFileEntry(ifstream& drv, const BS& bs, const string& filename) {


int rootDirStart = (bs.rsvdSecCnt + bs.numFATs * bs.secPerFAT) * bs.bps;
int numEntries = bs.rootEntCnt;
DirEntry entry;

drv.seekg(rootDirStart, drv.beg);
for (int i = 0; i < numEntries; ++i) {
drv.read(reinterpret_cast<char*>(&entry), sizeof(DirEntry));
if (entry.name[0] == 0x00) break;
if (filename == string(entry.name, entry.name + 11)) return entry;
}

throw runtime_error("File not found");


}

vector<uint16_t> getFileClusters(ifstream& drv, const BS& bs, const DirEntry& entry, const
vector<uint16_t>& fat) {
vector<uint16_t> clusters;
uint16_t cluster = entry.fstClusLO;

while (cluster < 0xFFF8) {


clusters.push_back(cluster);
cluster = fat[cluster];
}

return clusters;
}

int main() {
const char* drvPath = "\\\\.\\C:";
cout<<"Nhap vao ten file:" <<endl;
string filename ;
getline(cin,filename);
ifstream drv(drvPath, ios::binary);
if (!drv.is_open()) {
cerr << "Khong the mo o dia." << endl;
return 1;
}

BS bs = readBS(drv);
vector<uint16_t> fat = readFAT(drv, bs);

try {
DirEntry entry = findFileEntry(drv, bs, filename);
vector<uint16_t> clusters = getFileClusters(drv, bs, entry, fat);

cout << "Cac cluster cua tep " << filename << ":" << endl;
for (uint16_t cluster : clusters) {
cout << cluster << " ";
}
cout << endl;
} catch (const runtime_error& e) {
cerr << e.what() << endl;
}

drv.close();
return 0;
}
Bài 5
#include <iostream>
#include <fstream>
#include <cstdint>
#include <vector>
#include <stdexcept>

using namespace std;

struct BS {
uint8_t jmp[3];
char oem[8];
uint16_t bps;
uint8_t spc;
uint16_t rsvdSecCnt;
uint8_t numFATs;
uint16_t rootEntCnt;
uint16_t totSec16;
uint8_t media;
uint16_t secPerFAT;
uint16_t secPerTrk;
uint16_t numHeads;
uint32_t hiddSec;
uint32_t totSec32;
};

BS readBS(ifstream& drv) {
BS bs;
drv.read(reinterpret_cast<char*>(&bs), sizeof(BS));
return bs;
}

vector<uint16_t> readFAT(ifstream& drv, const BS& bs) {


int fatStart = bs.rsvdSecCnt * bs.bps;
vector<uint16_t> fat(bs.secPerFAT * bs.bps / sizeof(uint16_t));
drv.seekg(fatStart, drv.beg);
drv.read(reinterpret_cast<char*>(fat.data()), fat.size() * sizeof(uint16_t));
return fat;
}

int countEmptyClusters(const vector<uint16_t>& fat, int numClusters) {


int emptyCount = 0;
for (int i = 0; i < numClusters; ++i) {
if (fat[i] == 0) {
++emptyCount;
}
}
return emptyCount;
}

int main() {
const char* drvPath = "\\\\.\\C:";

ifstream drv(drvPath, ios::binary);


if (!drv.is_open()) {
cerr << "Khong the mo o dia." << endl;
return 1;
}

BS bs = readBS(drv);
vector<uint16_t> fat = readFAT(drv, bs);

int emptyClusterCount = countEmptyClusters(fat, 100);

cout << "So luong cluster trong trong 100 cluster dau tien: " << emptyClusterCount << endl;

drv.close();
return 0;
}

You might also like