Click here to Skip to main content
15,901,284 members
Articles / Programming Languages / C#
Tip/Trick

Standard Windows Text File Parser with Customized Text File Formatted Output into a New File

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
7 May 2024Public Domain1 min read 1.5K   14   3
Read a standard Windows Text File character by character
Purpose of this application is to show how to read a standard Windows Text File character by character and customize an output file using the data read in.

Program Load

Initial Run of this application

Open File Dialog

Open File Dialog

File Parsed

Sample File Parsed

Introduction

The purpose of this article is to show how easy it is to read in a Standard Windows Text File and customize your own output file from the read in text file. You can take this sample code then create your own reformatted text file based on your needs.

The author of this article used similar code to convert HTML data into text that was able to be copied and pasted into a Microsoft Visual Studio Code Web Page application based on selecting different buttons to a show different HTML menus.

Using the Code

Open the File Dialog to select a Standard Windows Text File to process and read in ALL of the Text File Data at one time into results character array:

C#
// Clear out any listed filename in the Open File Dialog that may have previously been shown, default to the root
// directory on drive C: in WIndows, and setup the default file type as .txt
openFromFileDialog.FileName = "";
openFromFileDialog.InitialDirectory = "C:\\";
openFromFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFromFileDialog.FilterIndex = 0;

// if OK was pressed, then handle the parsing of the data
if (openFromFileDialog.ShowDialog() == DialogResult.OK)
{
    FromFilename = openFromFileDialog.FileName;

    long fileSize = new FileInfo(FromFilename).Length;

    lbFromFilename.Text = FromFilename;

    // Create the Output File using the same directory as the Input File and replace the file extension with ".lst"
    ToFilename = Path.GetDirectoryName(FromFilename) + "\\" + Path.GetFileNameWithoutExtension(FromFilename) + ".lst";

    lbToFilename.Text = ToFilename;

    using (FileStream FromFileStream = File.Open(FromFilename, FileMode.Open))
    {
       using (BinaryReader br = new BinaryReader(FromFileStream, System.Text.Encoding.ASCII, false))
       {
          int fSize = System.Convert.ToInt32(fileSize.ToString());

          // Read in all characters into the character table results which we can then parse one character at a time
          char[] results = br.ReadChars(fSize);
...

Code follows that the code above adds header detail to the output file.

Create the output file as create only. Does not append to the file if it already exists! Then process the results data byte by byte.

C#
          // Do not append to the file if it already exists!
          StreamWriter sw = new StreamWriter(ToFilename, false, System.Text.Encoding.ASCII);
          sw.WriteLine("Filename: " + FromFilename);
          sw.WriteLine("File Size: " + fSize.ToString());
          sw.WriteLine("");
                            
          bool crFound = false; // reset to Carriage Return NOT found

          // Logic that parses the data and show it in the Input and Output ListViews!
          for (int i = 0; i < results.Length; i++)
          {
              // Get the next character one at a time
              string CurrentCharacter = results[i].ToString();

              // Line Feed? [Value of 10 decimal] which always follows a Carriage Return in normal text files!
              if (CurrentCharacter == "\n")
              {
                  rowIn = new DataGridViewRow();
                  rowIn.CreateCells(dataGridViewFromFile); // this line is important!
                  rowIn.Cells[0].Value = DataIn;
                  dataGridViewFromFile.Rows.Add(rowIn);
                  DataIn = "";

                  sw.WriteLine(DataOut);
                  rowOut = new DataGridViewRow();
                  rowOut.CreateCells(dataGridViewToFile); // this line is important!
                  rowOut.Cells[0].Value = DataOut;
                  dataGridViewToFile.Rows.Add(rowOut);
                  DataOut = "";
              }

              if (CurrentCharacter == "\r") // Carriage Return? [value of 13 decimal] 
              {
                 crFound = true;
              }
              else
              {
                 crFound = false;

                 // Do not add the line feed character to DataIn or DataOut!
                 if (CurrentCharacter != "\n")
                 {
                    // Add next character to the DataIn
                    DataIn += CurrentCharacter;

                    // Add next character to the DataOut
                    DataOut += CurrentCharacter;
                 }
              }
           }
...

The code that immediately follows the above code insures that if there is any held data not written yet that the code outputs that data then closes the Stream Writer for the output file.

Basic Principles

Data Parsing is a much needed thing these days but rarely seen.

Conclusion and Points of Interest

Parsing of data such as text files can be a very useful thing. The same concept could be used to process data from other sources such as hexadecimal data from Data Communications, etc.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Software Developer (Senior)
United States United States
BillNew - Software developer since November 1977.

Comments and Discussions

 
QuestionThe purpose for the article was not to optimize the code, etc. Pin
Destiny77713-May-24 10:13
Destiny77713-May-24 10:13 
BugQuestionable code practices Pin
Daniele Rota Nodari7-May-24 22:14
Daniele Rota Nodari7-May-24 22:14 
QuestionWhy didn't you use System.IO.TextReader? Pin
honey the codewitch7-May-24 14:36
mvahoney the codewitch7-May-24 14:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.