using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace Reading_txt_and_cs_files
{
public partial class Form1 : Form
{
List<string> methodNames = new List<string>();
public Form1()
{
InitializeComponent();
}
private void btnReadFromFile_Click(object sender, EventArgs e)
{
//create an open file dialog
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Filter = "txt files (*.txt)|*.txt|(*.cs)|*.cs";
//display it
DialogResult dr = fdlg.ShowDialog();
if (dr == DialogResult.OK)
{
//get the selected file and display in the
//textbox (txtFilePath)
txtFilePath.Text = fdlg.FileName;
}
}
private void btnReadFile_Click(object sender, EventArgs e)
{
StreamReader sr = null;
try
{
//1. Open file for reading
sr = File.OpenText(txtFilePath.Text);
//2. Read it
richTextBox1.Clear();
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
richTextBox1.AppendText(line + "\n");
string output = CountAllChars(line);
richTextBox2.AppendText(output);
}
}
catch (ArgumentException ae)
{
MessageBox.Show(ae.Message);
}
catch (IOException ioe)
{
MessageBox.Show(ioe.Message);
}
finally
{
//3. close
if (sr != null)
sr.Close();
}
}
static string CountAllChars(string s)
{
if (s == null) return null;
if (s == "") return "";
s = s.ToLower();
char[] chars = s.ToCharArray();
Array.Sort(chars);
StringBuilder sb = new StringBuilder();
int count = 0;
for (int i = 0; i < chars.Length; i++)
{
if (chars[i] < 'a' || chars[i] > 'z') continue;
if (sb.Length == 0)
{
sb = sb.Append(chars[i]);
count = 1;
}
else if (chars[i] == chars[i - 1])
{
count++;
}
else
{
sb = sb.Append(count.ToString());
sb = sb.Append("\n" + chars[i]);
count = 1;
}
}
sb = sb.Append(count.ToString());
return sb.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.Clear();
richTextBox2.Clear();
int ifcounter = 0;
int forcounter = 0;
StreamReader sr = null;
try
{
sr = File.OpenText(txtFilePath.Text);
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
richTextBox2.AppendText(line);
foreach (string c in line.Split())
{
if (c.Contains("if"))
{
if (true)
{
ifcounter++;
}
}
if (c.Contains("for"))
{
forcounter++;
}
}
}
richTextBox1.AppendText("Total if statement: " + ifcounter );
richTextBox1.AppendText("\n" + "Total for loop " + forcounter );
}
catch (ArgumentException ae)
{
MessageBox.Show(ae.Message);
}
catch (IOException ioe)
{
MessageBox.Show(ioe.Message);
}
finally
{
if (sr != null)
{
sr.Close();
}
}
}
}
}
//======================LAB ASSIGNMENT FOR MONDAY 6/16/14===========
//Write code to read a text file (.txt, .cs, or .java)
//and determine the number of occurrences of each character
//in the alphabet.
//Write code to read a .cs file and determine the number of for loops and if statements it has.
//forums.asp.net/t/1318256.aspx?Find+no+of+occurrence+of+each+character+in+a+string
//forums.asp.net/t/1608521.aspx?How+to+read+all+function+method+names+from+cs+file