0% found this document useful (0 votes)
11 views

Indexing Sectuion

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)
11 views

Indexing Sectuion

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/ 5

namespace File_Organization_Indexing

{
public partial class Form1 : Form
{
FileStream fs;
StreamWriter sw;
StreamReader sr;
StreamWriter isw;
StreamReader isr;
SortedDictionary<int, int> dic = new SortedDictionary<int,
int>();

public bool BST(int[] arr, int key)


{
int first = 0, last = arr.Length - 1;
while (last >= first)
{
int mid = (first + last) / 2;
if (arr[mid] > key)
first = mid + 1;
else if (arr[mid] > key)
last = mid - 1;
else
return true;
}
return false;
}
public Form1()
{
InitializeComponent();
}

//OPEN BUTTON
private void button1_Click(object sender, EventArgs e)
{
fs = new FileStream("test.txt", FileMode.OpenOrCreate,
FileAccess.ReadWrite);
sw = new StreamWriter(fs);
sr = new StreamReader(fs);
MessageBox.Show("Opened");
button2.Enabled = true;
}

//LOAD INDEX BUTTON


private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = "Key" + "\t" + "Location" + "\r\n";
dic.Clear();
if (File.Exists("index.txt"))
{
isr = new StreamReader("index.txt");
string line;
while ((line = isr.ReadLine()) != null)
{
string[] al = line.Split('|');
dic.Add(int.Parse(al[0]), int.Parse(al[1]));
textBox1.Text += al[0] + "\t" + al[1] + "\r\n";
}
isr.Close();
MessageBox.Show("Loaded");
}
button3.Enabled = true;
button4.Enabled = true;
button5.Enabled = true;
button6.Enabled = true;
button7.Enabled = true;
button8.Enabled = true;
button9.Enabled = true;
button1.Enabled = false;
button2.Enabled = false;
}
//INSERT BUTTON
private void button3_Click(object sender, EventArgs e)
{
if (textBox2.Text != "" && textBox3.Text != "" &&
textBox4.Text != "")
{
fs.Seek(0, SeekOrigin.End);
int h = Convert.ToInt16(fs.Position);
string data = textBox2.Text + "|" + textBox3.Text +
"|" + textBox4.Text;
sw.WriteLine(data);
sw.Flush();
dic.Add(int.Parse(textBox2.Text), h);
MessageBox.Show("Done");
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
}
else
MessageBox.Show("Enter all data fields");
}

//READ BUTTON
private void button4_Click(object sender, EventArgs e)
{
string line;
if ((line = sr.ReadLine()) != null)
{
string[] fields = line.Split('|');
textBox2.Text = fields[0];
textBox3.Text = fields[1];
textBox4.Text = fields[2];
}
else
fs.Seek(0, SeekOrigin.Begin);
}
//SEARCH BUTTON
private void button5_Click(object sender, EventArgs e)
{
sr.DiscardBufferedData();
int[] arr = dic.Keys.ToArray();
if (BST(arr, int.Parse(textBox2.Text)))
{
int l = dic[int.Parse(textBox2.Text)];
fs.Seek(l, SeekOrigin.Begin);
string line = sr.ReadLine();
string[] fields = line.Split('|');
textBox3.Text = fields[1];
textBox4.Text = fields[2];
MessageBox.Show("Found");
return;
}
MessageBox.Show("Not Found");
}

//DELETE BUTTON
private void button6_Click(object sender, EventArgs e)
{
sr.DiscardBufferedData();
int[] arr = dic.Keys.ToArray();
if (BST(arr, int.Parse(textBox2.Text)))
{
int l = dic[int.Parse(textBox2.Text)];
fs.Seek(l, SeekOrigin.Begin);
sw.Write("*");
sw.Flush();
dic.Remove(int.Parse(textBox2.Text));
MessageBox.Show("Deleted");
return;
}
MessageBox.Show("Not Found");
}

//REWRITE BUTTON
private void button7_Click(object sender, EventArgs e)
{
isw = new StreamWriter("index.txt");
foreach (KeyValuePair<int,int> item in dic)
{
isw.WriteLine(item.Key + "|" + item.Value);
isw.Flush();
}
isw.Close();
}
//CLEAR BUTTON
private void button8_Click(object sender, EventArgs e)
{
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
}

//CLOSE BUTTON
private void button9_Click(object sender, EventArgs e)
{
sw.Close();
fs.Close();
MessageBox.Show("Closed");
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button8.Enabled = false;
button9.Enabled = false;
button1.Enabled = true;
}
}
}

You might also like