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.
Initial Run of this application
Open File Dialog
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:
openFromFileDialog.FileName = "";
openFromFileDialog.InitialDirectory = "C:\\";
openFromFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFromFileDialog.FilterIndex = 0;
if (openFromFileDialog.ShowDialog() == DialogResult.OK)
{
FromFilename = openFromFileDialog.FileName;
long fileSize = new FileInfo(FromFilename).Length;
lbFromFilename.Text = FromFilename;
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());
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.
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;
for (int i = 0; i < results.Length; i++)
{
string CurrentCharacter = results[i].ToString();
if (CurrentCharacter == "\n")
{
rowIn = new DataGridViewRow();
rowIn.CreateCells(dataGridViewFromFile);
rowIn.Cells[0].Value = DataIn;
dataGridViewFromFile.Rows.Add(rowIn);
DataIn = "";
sw.WriteLine(DataOut);
rowOut = new DataGridViewRow();
rowOut.CreateCells(dataGridViewToFile);
rowOut.Cells[0].Value = DataOut;
dataGridViewToFile.Rows.Add(rowOut);
DataOut = "";
}
if (CurrentCharacter == "\r")
{
crFound = true;
}
else
{
crFound = false;
if (CurrentCharacter != "\n")
{
DataIn += CurrentCharacter;
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.