Operating system Experiment--file management __ Operating system

Source: Internet
Author: User
#include <iostream> #include <ctime> #include <cstdlib> #include <vector> #include <string
> Using namespace std; #define LEN 500/************************************************** index allocation + bit table based on variable length region ******************************


/* Definition file allocation table structure */typedef struct FAT//file Allocation table file allocation tables {string filename;//filename int indexblock;//index block number}fat;
	* * Randomly generated file size/float randfilesize () {//srand (Time (seed));
float fileSize = (rand ()% bayi +)/10.0f;//randomly produces a file of 2k-10k size for return fileSize; /* * Storage File/bool Storagefile (int bittable[],vector<fat> &fat, file file, int disk[][len]) {int n = ceil (fil
	E.SIZE/2);//Up, get the number of blocks (excluding the index block) required for the file store/check if there are enough empty blocks to hold the file int i = 0;
	int k = 0;
			int index = -1;//index block for (; i < LEN; i++) {if (bittable[i) = = 0) {k++;
			if (k = = 1) {index = i;//Note the index block} if (k = = n + 1) {break;
}
		}	///There is not enough space to hold the file if (k <= N) {return false;
	}//cout << "index block:" << index <<endl; //If there are enough blank blocks to hold the file, the index block is stored on the first blank block/////////////
	Update file allocation table fat fat;
	Fat.filename = File.name;
	Fat.indexblock = index;
	
	Fat.push_back (FAT);
	Update bit table Bittable[index] = 1;
	i = index + 1;//continues to look for blank blocks from the next position in the index block k = 0;//the length int t = 0 for each segment partition;
				Locate the stored location according to the bit table, update the index block, update the bit table for (int j = 0; J < N; j +) {if (bittable[i] = 0) {if (k = = 0) {//This is a new partition, record the start block
			disk[index][t++] = i;
			} k++;
				Bittable[i] = 1;//update bit table} else{if (k!= 0) {//If k!=0 description, the partition ends, record length disk[index][t++] = k;
				cout << "\ t start block:" << disk[index][t-2] << "t length:" << disk[index][t-1] << Endl;
			DISK[INDEX][T] = -1;//set a flag k = 0;
		} j--;
	} i++;
	} disk[index][t++] = k;
	DISK[INDEX][T] =-1; cout << "\ t startBlock: "<< disk[index][t-2] <<" \ t length: "<< disk[index][t-1] << endl << Endl;
return true; int Indexblockoffile (vector<fat> Fat, String fileName) {for (int i = 0; i < fat.size (); i++) {if (fat[i).
		filename = = filename) {return fat[i].indexblock; } return-1;//Return-1 description not found}/* Delete file update bit table */bool DeleteFile (int bittable[], vector<fat> &fat, String filen
			AME, int disk[][len]) {//index block found in file allocation table for (int i = 0; i < fat.size (); i++) {if (Fat[i].filename = fileName) {
			int index = Fat[i].indexblock;
			int t = 0;
				while (Disk[index][t]!=-1) {int begin = disk[index][t];
				int len = disk[index][t + 1];
				for (int j = 0; J < Len; J +) {Bittable[begin + j] = 0;
			} T + + 2;
			} Bittable[index] = 0;
			Vector<fat>::iterator ITR = Fat.begin () +i;
			Fat.erase (ITR);
		return true;
return false; /* Output index block and stored position/void Outputindexblock (int disk[][len], vector<fat> Fat, FilE file) {string fileName = File.name;
	int size = File.size;
	cout << fileName << "T size:" << size << "K" << Endl;
			for (int i = 0; i < fat.size (); i++) {if (Fat[i].filename = = fileName) {int index = Fat[i].indexblock;
			cout << "Index block:" << index << Endl;
			int t = 0;
				while (Disk[index][t]!=-1) {int begin = disk[index][t];
				int len = disk[index][t + 1];
				cout << "\ t start block:" << begin << "T length: << len << Endl;
			T + + 2;
		} break;
	}} * * print blank block/void Outputfreeblock (int bittable[]) {cout << "free block" << Endl;
		for (int i = 0; i < LEN; i++) {if (bittable[i] = = 0) {cout << i << "\ t";
}} cout << Endl; int main () {int Bittable[len] = {0};//bit table int Disk[len][len] = {1};//disk vector<fat> Fat;//file allocation table//random Generation 2
	k-10k file 50, and store it to disk in file file[50];
	String FileName;
	float fileSize; for (int i = 0; i < i++) {fileName = to_string (i+1) + ". txt"; FileName fileSize = randfilesize ();
		Randomly generated file size file[i].name = FileName;
		File[i].size = fileSize;
		cout << fileName << "" << fileSize << "T";   Store the file if (Storagefile (bittable, FAT, file[i], disk) {//cout << "successfully created the file:" << file[i].name << "
			Size: "<< file[i].size <<" K "<< Endl; Outputindexblock (disk, FAT, file[i]);
		cout << Endl;

	}//outputfreeblock (bittable);
	cout << "\-------------------" << Endl;
		Delete odd. txt for (int i = 1; i < i + + 2) {string fileName = to_string (i) + ". txt";
		if (!deletefile (bittable, FAT, FileName, disk)) break;
	cout << "Successfully deleted file:" << fileName << Endl;
	} cout << "\ n-------------------" << Endl;
	Outputfreeblock (bittable);
	Newly created 5 files file Filea, Fileb, Filec, FileD, Filee;	Filea.name = "A.txt";
	Filea.size = 7;	Fileb.name = "B.txt";
	Fileb.size = 5; 	Filec.name = "C.txt"; Filec.siZe = 2; 	Filed.name = "D.txt";
	Filed.size = 9; 	Filee.name = "E.txt";
	Filee.size = 3.5;
	/* Storagefile (bittable, FAT, Filea, disk);
	Storagefile (bittable, FAT, Fileb, disk);
	Storagefile (bittable, FAT, Filec, disk);
	Storagefile (bittable, FAT, FileD, disk);
	Storagefile (bittable, FAT, Filee, disk); */if (Storagefile (bittable, FAT, Filea, disk) {cout << "successfully created the file:" << filea.name << Size: "<&lt ;
	Filea.size << "K" << Endl;  } if (Storagefile (bittable, FAT, Fileb, disk)) {cout << "successfully created the file:" << fileb.name << Size: "<<
	Fileb.size << "K" << Endl;  } if (Storagefile (bittable, FAT, Filec, disk)) {cout << "successfully created the file:" << filec.name << Size: "<<
	Filec.size << "K" << Endl;  } if (Storagefile (bittable, FAT, FileD, disk)) {cout << "successfully created the file:" << filed.name << Size: "<<
	Filed.size << "K" << Endl; } if (Storagefile (bittable, FAT, Filee, DISK)) {cout << "successfully created the file:" << filee.name << "size: << filee.size <<" K "<< Endl;
	} cout << "\ n-------------------" << Endl;
	Give the file allocation table of these 5 files cout << "file allocation table:" << Endl;
	/* cout << filea.name << "index block is:" << indexblockoffile (FAT, filea.name) << Endl;
	cout << fileb.name << "index block is:" << indexblockoffile (FAT, fileb.name) << Endl;
	cout << filec.name << "index block is:" << indexblockoffile (FAT, filec.name) << Endl;
	cout << filed.name << "index block is:" << indexblockoffile (FAT, filed.name) << Endl;
	cout << filee.name << "index block is:" << indexblockoffile (FAT, filee.name) << Endl; * * Outputindexblock (disk, FAT, Filea);
	cout << Endl; Outputindexblock (disk, FAT, Fileb);
	cout << Endl; Outputindexblock (disk, FAT, Filec);
	cout << Endl; Outputindexblock (disk, FAT, FileD);
	cout << Endl; OutpuTindexblock (disk, FAT, Filee);
	cout << "\-------------------" << Endl;

	Output free block Outputfreeblock (bittable);
	System ("pause");
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.