How to read and write file using Stream Reader and Stream Writer?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Arjun Sarankulu
    New Member
    • Jan 2011
    • 4

    #1

    How to read and write file using Stream Reader and Stream Writer?

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    
    namespace PNB
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    if (openFileDialog1.FileName == "")
                    {
                        MessageBox.Show("File Not Selected", "Error");
    
                    } //if (openFileDialog1.FileName == "")
    
                    StreamWriter SW;
                    StreamReader SR;
                    //string S;
                    //string SingleLine = "";
                    string bn = "";
                    //if (rbPNB.Checked == true) { bn = "PNB"; } else { bn = "Ace"; }
                    
                    SR = File.OpenText(openFileDialog1.FileName);
                    //SW = File.CreateText("D:\\PNB\\" + bn + "1.txt");
                    SW = File.CreateText("D:\\PNB\\1.txt");
    
                    int[] numbers = {15, 18, 26, 27, 44, 74, 79, 99, 104, 114, 120, 128, 129, 146, 149, 154,
    				    169, 179, 184, 190 ,196, 198, 199, 211, 221, 241, 246, 276, 316, 356, 396,
    				    436, 476, 493, 510, 527, 544, 561, 591, 607, 619, 629, 639, 648, 652, 908
    				        }; //int[] numbers 
                    //string strpos = System.Configuration.ConfigurationManager.AppSettings["array"];
                    //char[] sep = { ',' };
                    //string[] arraynum = strpos.Split(sep);
    
                    //string strpos1 = System.Configuration.ConfigurationManager.AppSettings["array1"];
                    //char[] sep1 = { ',' };
                    //string[] arraynum1 = strpos1.Split(sep1);
    
                    //int[] number = { 10, 1, 4, 5, 5, 29, 30, 8, 6, 39 };//int[] number
    
                    string[] arr = { };//string[] arr
                    int n = numbers.Length;
                    int b = 0;
                    int c = 0;
                    int s;
                    int numberslength = numbers.Length;
                    //int numlength = arraynum.Length;
                    string SingleLine = "";
                    string S;
                    while ((SingleLine = SR.ReadLine()) != null)
                    //while (!SR.EndOfStream)               
                    {
                        //if (bn == "Ace")
                        //{
    
                        S = SR.ReadLine();
    
                        for (int i = 0; i < n; i++)
                        {
                            if (i == 0)
                            {
                                s = -1;
                                int a = numbers[i];
                                b = s + 1;
                                c = a - b + 1;
    
                                //Console.WriteLine("Start:\t" + b);
                                //Console.WriteLine("End:\n" + c);
                            }
                            else
                            {
                                //int k = numbers[i - 1];
                                int a = numbers[i];
                                b = numbers[i - 1] + 1;//start
                                c = a - b + 1;//End
                                //Console.WriteLine("Start:\t" + p);
                                //Console.WriteLine("End:\n" + q);
                            }
    
                            SingleLine = String.Concat(SingleLine, S.Substring(b, c));
                            SingleLine = String.Concat(SingleLine, ",");
    
    
                            //string SingleLine = String.Concat(SingleLine, S.Substring(b, c));
                            //SingleLine = String.Concat(SingleLine, ",");
    
                        }//for (int i = 0; i < n; i++)
    
                        string st = SingleLine;
    
                        arr = st.Split(',');
                        
                        int index;
                        string outputstring = "";
                        if (rbPNB.Checked == true)
                        {
                            bn = "PNB";
                            string strpos = System.Configuration.ConfigurationManager.AppSettings["array"];
                            char[] sep = { ',' };
                            string[] arraynum = strpos.Split(sep);
    
                            for (int j = 0; j < arraynum.Length; j++)
                            {
                                index = Int32.Parse(arraynum[j]) - 1;
                                outputstring += arr[index] + ",";
                            }
                        }//if (bn == "Ace")
                        
                        else
                        {
                            bn = "Ace";
                            string strpos1 = System.Configuration.ConfigurationManager.AppSettings["array1"];
                            char[] sep1 = { ',' };
                            string[] arraynum1 = strpos1.Split(sep1);
                            for (int j = 0; j < arraynum1.Length; j++)
                            {
                                index = Int32.Parse(arraynum1[j]) - 1;
                                outputstring += arr[index] + ",";
                            }
                        }
                        SingleLine = outputstring.Trim(',');
                        SW.WriteLine(SingleLine);
    
                        // }//if (bn == "Ace")                              
    
    
    
    
                    } // while ( !SR.EndOfStream ) 
    
                    SR.Close();
                    SW.Close();
    
                    MessageBox.Show("Sucessful File Conversion to " + bn);
                }//try
                catch(Exception er) {
                    MessageBox.Show("Error : "+ er.Message);
                }
            } // Private Button1 click
    
    
    
    
            private void button2_Click(object sender, EventArgs e)
            {
                while (openFileDialog1.FileName == "")
                {
                    openFileDialog1.ShowDialog();
                }//End of While Loop
    
    
            }//End Of button2_click
    
            private void rbPNB_CheckedChanged(object sender, EventArgs e)
            {
    
            }//End of rbPNB_checkedChanged
    
            private void rbAce_CheckedChanged(object sender, EventArgs e)
            {
    
            }//End of rbAce_checkedChanged
    
        }//End Of the class Form1
    
    }//End of Namespace PNB
    The above code accept file from file Dialog
    when i take txt file it convert the file to the respective format and if i take other than this format it creates the empty file and goes to the catch exception and display the following error in the Message Box -->"Error:Inde x and length must refer to a location within the string" Parameter name : Length
Working...