Reading and Writing to another file
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Reading and Writing to another file

  1. #1
    Join Date
    Jan 2011
    Posts
    10

    Reading and Writing to another file

    I am working on .net 4.0
    In my organisation there are two file format
    1)PNB and 2)ACE(need to select any one of the format to convert to respective format
    if i select ACE means through file browser i have selected PNB which will convert to ACE after submitting the button
    In PNB there are thousand character in one LINE which contain the data and i need to seperate and form a string and seperate this string by ,(comma) as delimiter.
    I have seperate the characters to string but the problem is that the string which are formed i need to pickup from middle and then write


    Following is my 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("This is the Error", "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");



    if (bn == "Ace")
    {
    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 s = -1;
    //int a, b, c;

    S = SR.ReadLine();
    while (S != null)
    {
    int n = numbers.Length;






    SingleLine = "";
    int i = 10;
    while (i <= 39)
    {
    int a = numbers[i];
    int p = numbers[i - 1] + 1;//start
    int q = a - p + 1;//End

    SingleLine = String.Concat(SingleLine,S.Substring(p,q));
    SingleLine = String.Concat(SingleLine, ",");
    //above logic will seperate char to string but i want to pick up characters present at this positions 114, 15, 27, 44, 44, 316, 356, 99, 74, 591 and form a string with seperate commas and this will form a line and this line i will write in ACE format which i have specified above
    and this should be for all line by line


    } // while (i <= 39)




    }








    //Console.WriteLine("Start\t"+b);
    //Console.WriteLine("Length\n"+c);
    }

    //SW.WriteLine(S);
    SR.Close();
    SW.Close();

    //S = SR.ReadLine();
    }
    //SR.Close();
    //SW.Close();
    MessageBox.Show("Done");
    //S = SR.ReadLine();
    //SingleLine = "";
    /*
    int n = numbers.Length;
    for (int i = 0; i < n; i++)
    {



    if (i == 0)
    {
    s = -1;
    int a = numbers[i];
    int b = s + 1;
    int c = a - b + 1;
    //Console.WriteLine("Start:\t" + b);
    //Console.WriteLine("End:\n" + c);
    }
    else
    {
    //int k = numbers[i - 1];
    int a = numbers[i];
    int p = numbers[i - 1] + 1;//start
    int q = a - p + 1;//End
    //Console.WriteLine("Start:\t" + p);
    //Console.WriteLine("End:\n" + q);

    if (i == 10 || i == 1)
    {

    SingleLine = string.Concat(SingleLine,S.Substring(p,q));

    }

    }







    //Console.WriteLine("Start\t"+b);
    //Console.WriteLine("Length\n"+c);
    }

    }



    S = SR.ReadLine();
    while (S != null)
    {

    SW.WriteLine(S);

    S = SR.ReadLine();
    }
    SR.Close();
    SW.Close();
    MessageBox.Show ("Done");*/
    }
    }

    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)
    {

    }
    }
    }


    //Can any one will help me for te same

  2. #2
    Join Date
    Jan 2011
    Posts
    11

    Re: Reading and Writing to another file

    Code:
    string AceFormatString = "";
    int[] AceFormatPoints = { 114, 15, 27, 44, 44, 316, 356, 99, 74, 591 };
    
    foreach (int i in AceFormatPoints)
    {
        if (i == 114)
            AceFormatString = line.Substring(i, 1);
        else
            AceFormatString += "," + line.Substring(i, 1);
    }

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center