Parse a text file based on column numbers c#

Posted on 2009-03-31
Last Modified: 2013-12-17
Hello Experts,

I have a large text file of data which is not delimited.  It appears that each field begins a specific number of characters into each line.

Like so:
Field001             Field002            Field003Field004       Field005
Field0010003     Field00234        Field003Field004       Field005
Field00123         Field0030          Field004Field004       Field005

I am comfortable workng with CSV and other delimited files.  What is the best way to clean this file up using C#?

Thanks for any help.
Question by:soapygus
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24035266

Accepted Solution

HarryNS earned 500 total points
ID: 24037195
Check this code. I have got this sometime back from online.

DataSet data = BuildDataSet("C:\\Test.txt","Table",",");
 #region BuildDataSet

        /// <summary>

        /// method to read a text file into a DataSet

        /// </summary>

        /// <param name="file">file to read from</param>

        /// <param name="tableName">name of the DataTable we want to add</param>

        /// <param name="delimeter">delimiter to split on</param>

        /// <returns>a populated DataSet</returns>

        public DataSet BuildDataSet(string file, string tableName, string delimeter)


            //create our DataSet

            DataSet domains = new DataSet();

            //add our table




                //first make sure the file exists

                if (File.Exists(file))


                    //create a StreamReader and open our text file

                    StreamReader reader = new StreamReader(file);

                    //read the first line in and split it into columns

                    string[] columns = reader.ReadLine().Split(delimeter.ToCharArray());

                    //now add our columns (we will check to make sure the column doesnt exist before adding it)

                    foreach (string col in columns)


                        //variable to determine if a column has been added

                        bool added = false;

                        string next = "";

                        //our counter

                        int i = 0;

                        while (!(added))


                            string columnName = col;

                            //now check to see if the column already exists in our DataTable

                            if (!(domains.Tables[tableName].Columns.Contains(columnName)))


                                //since its not in our DataSet we will add it

                                domains.Tables[tableName].Columns.Add(columnName, typeof(string));

                                added = true;




                                //we didnt add the column so increment out counter





                    //now we need to read the rest of the text file

                    string data = reader.ReadToEnd();

                    //now we will split the file on the carriage return/line feed

                    //and toss it into a string array

                    string[] rows = data.Split("\r".ToCharArray());

                    //now we will add the rows to our DataTable

                    foreach (string r in rows)


                        string[] items = r.Split(delimeter.ToCharArray());

                        //split the row at the delimiter






                    throw new FileNotFoundException("The file " + file + " could not be found");



            catch (FileNotFoundException ex)


                //_message = ex.Message;

                return null;


            catch (Exception ex)


                //_message = ex.Message;

                return null;


            //now return the DataSet

            return domains;



Open in new window


Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now