Friday, 25 March 2016

C# fileio workout


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace fileio1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
           
        }

        public FileStream file_location;

        public StreamReader reader1,reader2,reader3;

        public StreamWriter writter;

        private void button1_Click(object sender, EventArgs e) //read master
        {
            open_read(@"d:\master.txt", ref reader1);

            while (true)
            {
                string data = reader1.ReadLine();

                if (data == null) { break; }

                richTextBox1.Text += data + "\n";
            }

            close_read(ref reader1);
        }        

        private void button2_Click(object sender, EventArgs e) //read transaction
        {
            open_read(@"d:\transaction.txt", ref reader2);

            while (true)
            {
                string data = reader2.ReadLine();
                if (data == null) { break; }

                richTextBox2.Text += data + "\n";
            }

            close_read(ref reader2);
        }

        private void button3_Click(object sender, EventArgs e) //read new master file
        {

            open_read(@"d:\new_master.txt",ref reader3);

            while (true)
            {
                string data = reader3.ReadLine();
                if (data == null) { break; }

                richTextBox3.Text += data + "\n";
            }

            close_read(ref reader3);
        }

        private void button4_Click(object sender, EventArgs e) //write new master file
        {
            
        }

        public void print_to_new_master(int a, string b, double c)
        {
            open_write(@"d:\new_master.txt");
            string data = string.Format("{0,2:00}{1,-10}{2, -7:0000.00}",a,b,c);
            writter.Write(data+"\n");

            close_write();
        }

        public void open_read(string path,ref StreamReader reader)
        {
            file_location = new FileStream(path, FileMode.Open, FileAccess.Read);
            reader = new StreamReader(file_location);
        }

        public void close_read(ref StreamReader reader)
        {
            reader.Close();            
            file_location.Close();
        }

        public void open_write(string path)
        {
            file_location = new FileStream(path, FileMode.Append, FileAccess.Write);
            writter = new StreamWriter(file_location);
        }

        public void close_write()
        {
            writter.Close();
            file_location.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            open_read(@"d:\master.txt", ref reader1);
            open_read(@"d:\transaction.txt", ref reader2);

            int master_ID, transaction_ID;
            string master_name, transaction_name;
            double master_dollar, transaction_dollar;

            string master_data = reader1.ReadLine();    //read a master            

            get_master_info(master_data,out master_ID,out master_name,out master_dollar);

            string transaction_data = reader2.ReadLine(); //read a transaction

            get_transaction_info(transaction_data, out transaction_ID, out transaction_name, out transaction_dollar);

            while (master_data != null || transaction_data != null)
            {

                if (master_ID == transaction_ID)
                {
                    master_dollar += transaction_dollar;    //add transaction to master

                    transaction_data = reader2.ReadLine(); //read a transaction
                    if (transaction_data == null) { break; }

                    get_transaction_info(transaction_data, out transaction_ID, out transaction_name, out transaction_dollar);
                }
                else
                {
                    if (transaction_ID > master_ID)
                    {
                        print_to_new_master(master_ID, master_name, master_dollar); //copy master to new_master

                        master_data = reader1.ReadLine();    //read a master 
                        if (master_data == null) { break; }

                        get_master_info(master_data, out master_ID, out master_name, out master_dollar);
                    }
                    else
                    {
                        print_to_new_master(transaction_ID, transaction_name, transaction_dollar); //copy transaction to new_master

                        transaction_data = reader2.ReadLine(); //read a transaction
                        if (transaction_data == null) { break; }

                        get_transaction_info(transaction_data, out transaction_ID, out transaction_name, out transaction_dollar);
                    }
                }
            }

            while (master_data != null && transaction_data == null) //take care of the end
            {
                print_to_new_master(master_ID, master_name, master_dollar); //copy master to new_master

                master_data = reader1.ReadLine();    //read a master 
                if (master_data == null) { break; }

                get_master_info(master_data, out master_ID, out master_name, out master_dollar);
            }

            while (master_data == null && transaction_data != null) //take care of the end
            {
                print_to_new_master(transaction_ID, transaction_name, transaction_dollar); //copy transaction to new_master

                transaction_data = reader2.ReadLine(); //read a transaction
                if (transaction_data == null) { break; }

                get_transaction_info(transaction_data, out transaction_ID, out transaction_name, out transaction_dollar);
            }

            close_read(ref reader1);
            close_read(ref reader2);
            close_write();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            
        }

        private void button5_Click(object sender, EventArgs e) //clear content
        {
            richTextBox3.Text = "";
            file_location = new FileStream(@"d:\new_master.txt", FileMode.Truncate, FileAccess.Write);
            file_location.Close();
        }

        public void get_master_info(string everything, out int ID_, out string name_, out double dollar_)
        {
            ID_ = Convert.ToInt32(everything.Substring(0, 2));
            name_ = everything.Substring(2, 10);
            dollar_ = Convert.ToDouble(everything.Substring(12, 7));
        }

        public void get_transaction_info(string everything, out int ID_, out string name_, out double dollar_)
        {
            ID_ = Convert.ToInt32(everything.Substring(0, 2));
            name_ = everything.Substring(2, 10);
            dollar_ = Convert.ToDouble(everything.Substring(27, 7));
        }
    }
    
}

No comments:

Post a Comment