473,468 Members | 1,365 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

4 New Member
Expand|Select|Wrap|Line Numbers
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10.  
  11. namespace PNB
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.  
  20.         private void Form1_Load(object sender, EventArgs e)
  21.         {
  22.  
  23.         }
  24.  
  25.         private void button1_Click(object sender, EventArgs e)
  26.         {
  27.             try
  28.             {
  29.                 if (openFileDialog1.FileName == "")
  30.                 {
  31.                     MessageBox.Show("File Not Selected", "Error");
  32.  
  33.                 } //if (openFileDialog1.FileName == "")
  34.  
  35.                 StreamWriter SW;
  36.                 StreamReader SR;
  37.                 //string S;
  38.                 //string SingleLine = "";
  39.                 string bn = "";
  40.                 //if (rbPNB.Checked == true) { bn = "PNB"; } else { bn = "Ace"; }
  41.  
  42.                 SR = File.OpenText(openFileDialog1.FileName);
  43.                 //SW = File.CreateText("D:\\PNB\\" + bn + "1.txt");
  44.                 SW = File.CreateText("D:\\PNB\\1.txt");
  45.  
  46.                 int[] numbers = {15, 18, 26, 27, 44, 74, 79, 99, 104, 114, 120, 128, 129, 146, 149, 154,
  47.                     169, 179, 184, 190 ,196, 198, 199, 211, 221, 241, 246, 276, 316, 356, 396,
  48.                     436, 476, 493, 510, 527, 544, 561, 591, 607, 619, 629, 639, 648, 652, 908
  49.                         }; //int[] numbers 
  50.                 //string strpos = System.Configuration.ConfigurationManager.AppSettings["array"];
  51.                 //char[] sep = { ',' };
  52.                 //string[] arraynum = strpos.Split(sep);
  53.  
  54.                 //string strpos1 = System.Configuration.ConfigurationManager.AppSettings["array1"];
  55.                 //char[] sep1 = { ',' };
  56.                 //string[] arraynum1 = strpos1.Split(sep1);
  57.  
  58.                 //int[] number = { 10, 1, 4, 5, 5, 29, 30, 8, 6, 39 };//int[] number
  59.  
  60.                 string[] arr = { };//string[] arr
  61.                 int n = numbers.Length;
  62.                 int b = 0;
  63.                 int c = 0;
  64.                 int s;
  65.                 int numberslength = numbers.Length;
  66.                 //int numlength = arraynum.Length;
  67.                 string SingleLine = "";
  68.                 string S;
  69.                 while ((SingleLine = SR.ReadLine()) != null)
  70.                 //while (!SR.EndOfStream)               
  71.                 {
  72.                     //if (bn == "Ace")
  73.                     //{
  74.  
  75.                     S = SR.ReadLine();
  76.  
  77.                     for (int i = 0; i < n; i++)
  78.                     {
  79.                         if (i == 0)
  80.                         {
  81.                             s = -1;
  82.                             int a = numbers[i];
  83.                             b = s + 1;
  84.                             c = a - b + 1;
  85.  
  86.                             //Console.WriteLine("Start:\t" + b);
  87.                             //Console.WriteLine("End:\n" + c);
  88.                         }
  89.                         else
  90.                         {
  91.                             //int k = numbers[i - 1];
  92.                             int a = numbers[i];
  93.                             b = numbers[i - 1] + 1;//start
  94.                             c = a - b + 1;//End
  95.                             //Console.WriteLine("Start:\t" + p);
  96.                             //Console.WriteLine("End:\n" + q);
  97.                         }
  98.  
  99.                         SingleLine = String.Concat(SingleLine, S.Substring(b, c));
  100.                         SingleLine = String.Concat(SingleLine, ",");
  101.  
  102.  
  103.                         //string SingleLine = String.Concat(SingleLine, S.Substring(b, c));
  104.                         //SingleLine = String.Concat(SingleLine, ",");
  105.  
  106.                     }//for (int i = 0; i < n; i++)
  107.  
  108.                     string st = SingleLine;
  109.  
  110.                     arr = st.Split(',');
  111.  
  112.                     int index;
  113.                     string outputstring = "";
  114.                     if (rbPNB.Checked == true)
  115.                     {
  116.                         bn = "PNB";
  117.                         string strpos = System.Configuration.ConfigurationManager.AppSettings["array"];
  118.                         char[] sep = { ',' };
  119.                         string[] arraynum = strpos.Split(sep);
  120.  
  121.                         for (int j = 0; j < arraynum.Length; j++)
  122.                         {
  123.                             index = Int32.Parse(arraynum[j]) - 1;
  124.                             outputstring += arr[index] + ",";
  125.                         }
  126.                     }//if (bn == "Ace")
  127.  
  128.                     else
  129.                     {
  130.                         bn = "Ace";
  131.                         string strpos1 = System.Configuration.ConfigurationManager.AppSettings["array1"];
  132.                         char[] sep1 = { ',' };
  133.                         string[] arraynum1 = strpos1.Split(sep1);
  134.                         for (int j = 0; j < arraynum1.Length; j++)
  135.                         {
  136.                             index = Int32.Parse(arraynum1[j]) - 1;
  137.                             outputstring += arr[index] + ",";
  138.                         }
  139.                     }
  140.                     SingleLine = outputstring.Trim(',');
  141.                     SW.WriteLine(SingleLine);
  142.  
  143.                     // }//if (bn == "Ace")                              
  144.  
  145.  
  146.  
  147.  
  148.                 } // while ( !SR.EndOfStream ) 
  149.  
  150.                 SR.Close();
  151.                 SW.Close();
  152.  
  153.                 MessageBox.Show("Sucessful File Conversion to " + bn);
  154.             }//try
  155.             catch(Exception er) {
  156.                 MessageBox.Show("Error : "+ er.Message);
  157.             }
  158.         } // Private Button1 click
  159.  
  160.  
  161.  
  162.  
  163.         private void button2_Click(object sender, EventArgs e)
  164.         {
  165.             while (openFileDialog1.FileName == "")
  166.             {
  167.                 openFileDialog1.ShowDialog();
  168.             }//End of While Loop
  169.  
  170.  
  171.         }//End Of button2_click
  172.  
  173.         private void rbPNB_CheckedChanged(object sender, EventArgs e)
  174.         {
  175.  
  176.         }//End of rbPNB_checkedChanged
  177.  
  178.         private void rbAce_CheckedChanged(object sender, EventArgs e)
  179.         {
  180.  
  181.         }//End of rbAce_checkedChanged
  182.  
  183.     }//End Of the class Form1
  184.  
  185. }//End of Namespace PNB
  186.  
  187.  
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:Index and length must refer to a location within the string" Parameter name : Length
Jan 21 '11 #1
0 1357

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Simon Wigzell | last post by:
I recently wrote a program with MS Visual Studio C++, sent it off to the client where it didn't run, after some probing I discover they are on a Mac! My program is a MSF interface that is really...
2
by: Smoothy | last post by:
Hi all, Can someone help me with the following issue? I want to read/write from/to a particular position (line/character) of a text file. How can I do this? In other words, how can I tell to...
1
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each...
1
by: Nadja Schmitt | last post by:
Hi! Is it possible to read/write a *.resx-file? The problem: I'd like to add some more columns to my resource-file, than read the data out of the file and write the data in this file. I...
1
by: gce | last post by:
Hi, As a newbie (old vb6 programmer) I like to know how to read/write/lock a file using asp.net ? Best regards, Gert
0
by: ÀÏÓà | last post by:
i just use the following simple code for test, when the code line run to flush()/close(),the simple web process will often down ! i check the web server event log ,the "sc-status sc-substatus...
1
by: David Arden Stevensonn | last post by:
Say I have an XML file on my website that gets read alot (by a c# aspx page) but written to occasionally (also by the same c# aspx page) . Its a simple caching situation based on time. Example: If...
10
by: lorenzogordon | last post by:
Hi there, I'd greatly appreciate any insights into the following problem: I've got PHP running fine on IIS (OS: Server 2003, SP1; IIS: 6.0; PHP: 4.3.11). In PHP, the user uploads a file,...
0
by: Frederic Rentsch | last post by:
Hi all, Working with read and write operations on a file I stumbled on a complication when writes fail following a read to the end. 30L 'abcdefg' Traceback (most recent call last): File...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.