Share via


read file.txt and split by delimitate

Question

Monday, April 9, 2012 8:37 AM

Hi:

I'm building an application in C#. I have done the coding for reading a text file the code is below:

StreamReader file = new StreamReader(@'C:\Users\farshad\Desktop\1.txt')

, the content of this file is below : 

1^2^3^4^5^6^7^8^9^10
a^b^c^d^e^f^g^h^i^j

Now I want to split each of the Char by the deliminator ('^') and put it in a table. each char in a separate cell of table. 

anybody know how can I do this or guideline to achieve this?

tnx

All replies (10)

Thursday, April 12, 2012 6:02 AM âś…Answered | 1 vote

Hi Farshad,

I have same questions as Mitja. I tested your code on my side. NOTE: finally the table only contain one column and 20 rows. If what you want is two rows and 10 columns, please use following code:

private void button1_Click(object sender, EventArgs e)
        {
            DataTable table = new DataTable();

            using (StreamReader sr = new StreamReader(@"C:\Users\farshad\Desktop\1.txt"))
            {
                Int32 columns = 0;
                Int32 rows = 0;
                String line;
                while ((line = sr.ReadLine()) != null)
                {
                    table.Rows.Add();
                    String[] data = line.Split('^');
                    for (Int32 i = 0; i < data.Length; i++)
                    {
                        if (rows == 0)
                            table.Columns.Add(new DataColumn(String.Format("column{0}", columns), typeof(String)));
                        table.Rows[rows][columns] = data[i];
                        columns++;
                    }
                    rows++;
                    columns = 0;
                }
            }
        }

Second, if you want to show the table, you can use something like DataGridView control. First, you can drag and drop a DataGridView control from Toolbox to your form in Design windows. Then bind the data source in button1_Click  method. Here is an simply integrated code sample I write for you to test, please check it out:

private void button1_Click(object sender, EventArgs e)
        {
            DataTable table = new DataTable();

            using (StreamReader sr = new StreamReader(@"C:\Users\farshad\Desktop\1.txt"))
            {
                Int32 columns = 0;
                Int32 rows = 0;
                String line;
                while ((line = sr.ReadLine()) != null)
                {
                    table.Rows.Add();
                    String[] data = line.Split('^');
                    for (Int32 i = 0; i < data.Length; i++)
                    {
                        if (rows == 0)
                            table.Columns.Add(new DataColumn(String.Format("column{0}", columns), typeof(String)));
                        table.Rows[rows][columns] = data[i];
                        columns++;
                    }
                    rows++;
                    columns = 0;
                }
                dataGridView1.DataSource = table;
            }
        }

I hope this helps.

Best Regards,

Alexander Sun [MSFT]
MSDN Community Support | Feedback to us


Monday, April 9, 2012 8:47 AM | 1 vote

Now I want to split each of the Char by the deliminator ('^') and put it in a table. each char in a separate cell of table. 

Into a new column, or into a new row?

Mitja


Monday, April 9, 2012 8:52 AM

I want to assign each line to a row. here I have two line so I want two row in my table. and each character into one cell. it means 2 row and 10 column.

Tnx for your respond


Monday, April 9, 2012 8:54 AM | 2 votes

Here are both version:

1. for inseritng all data into one row (multiple columns)

2. for inserting data itno one column (multiple rows)

           DataTable table =new DataTable();

            // 1. for putting char into a new column (cell):
            using (StreamReader sr = new StreamReader(@"C:\Users\farshad\Desktop\1.txt"))
            {
                table.Rows.Add();
                int counter = 0;
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    string[] data = line.Split('^');
                    for (int i = 0; i < data.Length; i++)
                    {
                        table.Columns.Add(new DataColumn(string.Format("column{0}", counter), typeof(string)));
                        table.Rows[0][counter] = data[i];
                        counter++;
                    }
                }
            }

            // 2. for putting char into a new row (cell):
            using (StreamReader sr = new StreamReader(@"C:\Users\farshad\Desktop\1.txt"))
            {
                table.Columns.Add(new DataColumn("column 1", typeof(string)));
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    string[] data = line.Split('^');
                    for (int i = 0; i < data.Length; i++)
                    {
                        table.Rows.Add(data[i]);
                    }
                }
            }

choose one or the other.

Hope it helps,

bye

Mitja


Monday, April 9, 2012 8:58 AM | 1 vote

I see, this is now different approach. Based on your last description, you have to do it this way:

            DataTable table =new DataTable();

            // 1. for putting char of each row into a new row (chars of one row into each column):
            using (StreamReader sr = new StreamReader(@"C:\Users\farshad\Desktop\1.txt"))
            {
                int columns = 0;
                int rows = 0;
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    table.Rows.Add();
                    string[] data = line.Split('^');
                    for (int i = 0; i < data.Length; i++)
                    {
                        if (rows == 0)
                            table.Columns.Add(new DataColumn(string.Format("column{0}", columns), typeof(string)));
                        table.Rows[0][columns] = data[i];
                        columns++;
                    }
                    rows++;
                    columns = 0;
                }
            }

Mitja


Monday, April 9, 2012 9:07 AM

firstly I am so thankful for your help and support and attention. Your code is really helpful for me. but I don't know I already used your code into my application but it's not working. I am using Windows form application. I assigned the code to Button that by clicking button your code start to compile but nothing happen after I press the button do you know what is the problem for? this the code of my application.:

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

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

        private void button1_Click(object sender, System.EventArgs e)
        {

            DataTable table = new DataTable();
            
            using (StreamReader sr = new StreamReader(@"C:\Users\farshad\Desktop\1.txt"))
            {
                table.Columns.Add(new DataColumn("column 1", typeof(string)));
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    string[] data = line.Split('^');
                    for (int i = 0; i < data.Length; i++)
                    {
                        table.Rows.Add(data[i]);
                    }
                }

            }

        }
    }
}


Monday, April 9, 2012 9:27 AM | 1 vote

Nothing happening? Would you like to show the data from dataTable somewhere, like in datagridview control?

TIP: put a breakpoint before this code, and go through it line by line, you will see there that dataTable is filling up.

And USE my last code snippet.

Mitja


Monday, April 9, 2012 5:40 PM | 1 vote

PLease check following link, it may help you

http://stackoverflow.com/questions/1028224/c-sharp-read-text-file-containing-data-delimited-by-tabs

http://www.dotnetperls.com/split


Thursday, April 12, 2012 6:55 AM | 1 vote

      string[] fileLines = File.ReadAllLines(fileName);
      string[][] rowsCols = new string[fileLines.Length][];
      for (int i = 0; i < fileLines.Length; i++)
        rowsCols[i] = fileLines[i].Split('^');   

Thursday, April 12, 2012 7:03 AM | 1 vote

You need to create a datarow to insert in the table.

var x = table.NewRow();
x[0] = data[i];
table.Rows.Add(x);

Be aware the base of this is the sample from Mitja.

(Typed in this message so watch typos or whatever)

Success
Cor