CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2011
    Posts
    10

    Reading and Writing to the file using Stream Reader and Stream Writer

    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 {
    MessageBox.Show("Error : ");
    }
    } // 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



    Hi all,
    Above contain the code for reading the data and write from one format to another and vice versa
    But the problem is that at runtime, here suppose one file is in T004 format and i am writing to txt format it creates the empty file and it goes to catch block giving error which is present in the MessageBox.show and creates the empty file. and when i take txt file it sucessfully convert to respective format sucessfully
    Can anyone help with the same
    Thanks and Regards

  2. #2
    Join Date
    Dec 2007
    Posts
    234

    Re: Reading and Writing to the file using Stream Reader and Stream Writer

    1) Next time please us [code][/code] tags around your code to preserve the formatting. as it is, it's hard to follow.
    2) Where does the error happen? Try noting with a comment or [b][/b] tags to mark the line with the error.
    3) What is the error? That's nice that it's displayed in the messagebox, but we're not sitting at your computer so we can't see what the error is.

    -tg
    * I don't respond to private requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help - how to remove eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to???
    * On Error Resume Next is error ignoring, not error handling(tm). * Use Offensive Programming, not Defensive Programming.
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN
    MVP '06-'10

  3. #3
    Join Date
    Jan 2011
    Posts
    10

    Re: Reading and Writing to the file using Stream Reader and Stream Writer

    I have written try catch block
    It goes in the catch block
    and Print "error:" as msg in message box
    Thank for your comments

  4. #4
    Join Date
    Dec 2008
    Posts
    144

    Re: Reading and Writing to the file using Stream Reader and Stream Writer

    Quote Originally Posted by Arjun-Sarankulu View Post
    I have written try catch block
    It goes in the catch block
    and Print "error:" as msg in message box
    Thank for your comments
    Exactly what error are you getting? Please be specific with your question so we can provide help. Also, please point out where in that giant string of code the error is being raised.

  5. #5
    Join Date
    Jan 2011
    Posts
    10

    Re: Reading and Writing to the file using Stream Reader and Stream Writer

    Thanks Sir for your comment
    Its giving the exception and goes to catch block and display the respective error message present in Message Box.
    I want to say that if i am taking txt file from file browser it convert to the respective format.
    But for eg. file type is T004 ,etc it create the emty file and display the message present in catch block
    I think the problem is related to text format of the file(Encoding problem,think so).
    Can any one help what to be standardised

  6. #6
    Join Date
    Dec 2007
    Posts
    234

    Re: Reading and Writing to the file using Stream Reader and Stream Writer

    That loud banging you hear is me banging my head on the desk. Yes, we know that it is displaying the respective error in a message box. We can see that much from the code. The question is WHAT is the TEXT of the ERROR MESSAGE that is being displayed? Does it say "I'm sorry Dave I cannot do that." Or does it say "Remove eels from your hovercraft to continue." Or does it say a bunch of curse words? OR does it complain that the wheels fell off the car? How about a screen shot of the message box? That would help.

    -tg
    * I don't respond to private requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help - how to remove eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to???
    * On Error Resume Next is error ignoring, not error handling(tm). * Use Offensive Programming, not Defensive Programming.
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN
    MVP '06-'10

  7. #7
    Join Date
    Jan 2011
    Posts
    10

    Re: Reading and Writing to the file using Stream Reader and Stream Writer

    Below attached file contain the error
    Sorry for troubling you all
    Thanks and Regards
    Attached Images Attached Images  

  8. #8
    Join Date
    Dec 2007
    Posts
    234

    Re: Reading and Writing to the file using Stream Reader and Stream Writer

    OK, now we're getting somewhere. Was that so hard? OK... two things... 1) Could you PLEASE repost your code, this time with [code][/code] tags around it so that it preserves the indenting? I am having trouble following your code and seeing where things start and end. Secondly, can you remove (or comment out) the Try/catch? That way when it errors out, it should go into debug mode and show you what line is causing the error. You have so much code in the try at the moment, I'm not sure where to begin to look for the error. Once you know what line the error happens on, either post the line, or mark it in the code with a comment or highlight or change it's color somehow.

    Thanks,
    -tg
    * I don't respond to private requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help - how to remove eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to???
    * On Error Resume Next is error ignoring, not error handling(tm). * Use Offensive Programming, not Defensive Programming.
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN
    MVP '06-'10

  9. #9
    Join Date
    Jan 2011
    Posts
    10

    Re: Reading and Writing to the 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");
    
                    }
                    StreamWriter SW;
                    StreamReader SR;
                    string bn = "";
                    
                    SR = File.OpenText(openFileDialog1.FileName);
                    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

  10. #10
    Join Date
    Jan 2011
    Posts
    10

    Re: Reading and Writing to the 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)
            {
                     if (openFileDialog1.FileName == "")
                    {
                        MessageBox.Show("File Not Selected", "Error");
                    }
                    StreamWriter SW;
                    StreamReader SR;
                    string bn = "";
                    
                    SR = File.OpenText(openFileDialog1.FileName);
                    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
    				        };
                    string[] arr = { };
                    int n = numbers.Length;
                    int b = 0;
                    int c = 0;
                    int s;
                    int numberslength = numbers.Length;
                    string SingleLine = "";
                    string S;
                    while ((SingleLine = SR.ReadLine()) != null)
                    {
    
    
                        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;
    
    
                            }
                            else
                            {
                                int a = numbers[i];
                                b = numbers[i - 1] + 1;
                                c = a - b + 1;
    
                            }
    
                            SingleLine = String.Concat(SingleLine, S.Substring(b, c));
                            SingleLine = String.Concat(SingleLine, ",");
    
    
    
                        }
                        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] + ",";
                            }
                        }                    
                        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);
    
                        
    
    
    
                    } 
                    SR.Close();
                    SW.Close();
    
                    MessageBox.Show("Sucessful File Conversion to " + bn);
                          
            } 
    
    
    
            private void button2_Click(object sender, EventArgs e)
            {
                while (openFileDialog1.FileName == "")
                {
                    openFileDialog1.ShowDialog();
                }
    
            }
            private void rbPNB_CheckedChanged(object sender, EventArgs e)
            {
    
            }
            private void rbAce_CheckedChanged(object sender, EventArgs e)
            {
    
            }
        }
    }

    Thanks Sir
    Above is the code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured