65.9K
CodeProject is changing. Read more.
Home

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

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1 vote)

May 7, 2024

Public Domain

1 min read

viewsIcon

2340

downloadIcon

31

Read a standard Windows Text File character by character

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:

// 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.

          // 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.