Author Topic: Read file, write file  (Read 11148 times)

0 Members and 1 Guest are viewing this topic.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Read file, write file
« Reply #15 on: February 07, 2008, 03:21:10 PM »
I'm such a noob I couldn't even get it to print to the command line.  Here is my donation.
Code: [Select]
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace WindosApps
{
/// <summary>
/// Description of ReadFile.
/// </summary>
public class ReadFile
{

[STAThread]
public static void Main()
{
string FullFile;
Dictionary<char, int> Dict = new Dictionary<char, int>();
using (StreamReader sr = new StreamReader("c:/test.txt")) {
FullFile = sr.ReadToEnd();
}
foreach (char chr in FullFile.ToCharArray()) {
if (Dict.ContainsKey(chr))
Dict[chr] = ++Dict[chr];
else
Dict.Add(chr, 1);
}
StringBuilder sb = new StringBuilder();
foreach (KeyValuePair<char, int> kvp in Dict) {
//Console.WriteLine("Letter {0} appears {1} time(s).", kvp.Key, kvp.Value);
sb.Append("\n");
sb.Append(kvp.Key.ToString());
sb.Append(" [ ");
sb.Append(kvp.Value.ToString());
sb.Append(" ]");
}
//Console.WriteLine("\n Hit enter to continue.");
//Console.ReadLine();
MessageBox.Show(sb.ToString());
}
}
}
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Read file, write file
« Reply #16 on: February 07, 2008, 03:38:54 PM »
I'm such a noob I couldn't even get it to print to the command line.  Here is my donation.
Autocad command line or console cmd line?
This is not inside autocad, and doesn't have the autocad references needed to print to cmd line there.

If you knew this, just ignore me
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Read file, write file
« Reply #17 on: February 07, 2008, 03:43:05 PM »
I'm such a noob I couldn't even get it to print to the command line.  Here is my donation.
Autocad command line or console cmd line?
This is not inside autocad, and doesn't have the autocad references needed to print to cmd line there.

If you knew this, just ignore me
I was just doing it as a windows based program, so no Acad references.

Here is a better version.  It will only allow characters between 0-9, A-Z and a-z, and the dictionary, by definition, is sorted.
Code: [Select]
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace WindosApps
{
/// <summary>
/// Description of ReadFile.
/// </summary>
public class ReadFile
{

[STAThread]
public static void Main()
{
string FullFile;
//Dictionary<char, int> Dict = new Dictionary<char, int>();
SortedDictionary<char, int> Dict = new SortedDictionary<char, int>();
using (StreamReader sr = new StreamReader("c:/Raster2006.log")) {
FullFile = sr.ReadToEnd();
}
int tempInt;
foreach (char chr in FullFile.ToCharArray()) {
tempInt = (int)chr;
if
(tempInt < 91 && tempInt > 64
||
tempInt < 123 && tempInt > 96
||
tempInt < 58 && tempInt > 47
)
{
if (Dict.ContainsKey(chr))
Dict[chr] = ++Dict[chr];
else
Dict.Add(chr, 1);
}
}
StringBuilder sb = new StringBuilder();
foreach (KeyValuePair<char, int> kvp in Dict) {
//Console.WriteLine("Letter {0} appears {1} time(s).", kvp.Key, kvp.Value);
sb.Append("\n");
sb.Append(kvp.Key.ToString());
sb.Append(" [ ");
sb.Append(kvp.Value.ToString());
sb.Append(" ]");
}
//Console.WriteLine("\n Hit enter to continue.");
//Console.ReadLine();
MessageBox.Show(sb.ToString());
}
}
}
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Read file, write file
« Reply #18 on: February 07, 2008, 04:02:43 PM »
OK, I can see from the submissions, I definitly need t learn how to use Dict objects.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Read file, write file
« Reply #19 on: February 08, 2008, 12:32:51 PM »
OK, thanks everybody for the input.  I have a working bit of code using the Console, now I would like to move to a windows form.  Let me say I have NEVER done a windows form in C# other than HelloWorld.  Im thinking this should be pretty straight forward, but I want to be able to pick a file from the form.  I'll keep you posted on my progress.
« Last Edit: February 08, 2008, 02:07:19 PM by CmdrDuh »
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Read file, write file
« Reply #20 on: February 08, 2008, 12:33:19 PM »
For those playing along, here is my code so far
Code: [Select]
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Signage
{
    class Program
    {
        public static void Main(string[] args)
        {
            string FullFile;
            string flInput;
            string flOutput;
            Console.WriteLine("Enter File Name to process with a / slash");
            flInput = Console.ReadLine();
            Console.WriteLine("Enter File Name to Output to with a / slash");
            flOutput = Console.ReadLine();
            StreamWriter sw = new StreamWriter(flOutput);
            SortedDictionary<char, int> Dict = new SortedDictionary<char, int>();
            using (StreamReader sr = new StreamReader(flInput))
            {
                FullFile = sr.ReadToEnd();
            }
            int tempInt;
            foreach (char chr in FullFile.ToCharArray())
            {
                tempInt = (int)chr;
                if
                    (tempInt < 91 && tempInt > 64
                     ||
                     tempInt < 123 && tempInt > 96
                     ||
                     tempInt < 58 && tempInt > 31 //47  Changed to pick up the ()'. and space characters
                    )
                {
                    if (Dict.ContainsKey(chr))
                        Dict[chr] = ++Dict[chr];
                    else
                        Dict.Add(chr, 1);
                }
            }

            foreach (KeyValuePair<char, int> kvp in Dict)
            {
                StringBuilder sb = new StringBuilder();
                //sb.Append("\n");   Removed b/c it was not needed using WriteLine and it was creating unknown char in txt file
                sb.Append("Character: ");
                sb.Append(kvp.Key.ToString());
                sb.Append(" - [ ");
                sb.Append(kvp.Value.ToString());
                sb.Append(" ]");
                sw.WriteLine(sb);
                sw.Flush();
            }
            sw.Close();
        }
    }
}
« Last Edit: February 08, 2008, 12:38:20 PM by CmdrDuh »
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Read file, write file
« Reply #21 on: February 08, 2008, 02:04:16 PM »
Here is the windows form version
Code: [Select]
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SubStationSignage
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnFileToProcess_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 0;
            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.FileName = string.Empty;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtFileToProcess.Text  = openFileDialog1.FileName;
            }
        }

        private void btnFileToCreate_Click(object sender, EventArgs e)
        {
            saveFileDialog1.InitialDirectory = "c:\\";
            saveFileDialog1.FileName = string.Empty;
            saveFileDialog1.DefaultExt = "txt";
            saveFileDialog1.Filter = "txt files (*.txt)|*.txt";

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtFileToCreate.Text = saveFileDialog1.FileName;
            }
        }

        private void btnBegin_Click(object sender, EventArgs e)
        {
            if ((txtFileToProcess.Text != string.Empty) && (System.IO.File.Exists(txtFileToProcess.Text)) && (txtFileToCreate.Text != string.Empty))
            {
                if (System.IO.File.Exists(txtFileToCreate.Text))
                {
                    System.IO.File.Delete(txtFileToCreate.Text);
                }
                string FullFile;
                string flInput = txtFileToProcess.Text;
                string flOutput = txtFileToCreate.Text;
                StreamWriter sw = new StreamWriter(flOutput);
                SortedDictionary<char, int> Dict = new SortedDictionary<char, int>();
                using (StreamReader sr = new StreamReader(flInput))
                {
                    FullFile = sr.ReadToEnd();
                }
                int tempInt;
                foreach (char chr in FullFile.ToCharArray())
                {
                    tempInt = (int)chr;
                    if
                        (tempInt < 91 && tempInt > 64
                         ||
                         tempInt < 123 && tempInt > 96
                         ||
                         tempInt < 58 && tempInt > 31 //47
                        )
                    {
                        if (Dict.ContainsKey(chr))
                            Dict[chr] = ++Dict[chr];
                        else
                            Dict.Add(chr, 1);
                    }
                }

                foreach (KeyValuePair<char, int> kvp in Dict)
                {
                    StringBuilder sb = new StringBuilder();
                    //sb.Append("\n");
                    sb.Append("Character: ");
                    sb.Append(kvp.Key.ToString());
                    sb.Append(" - [ ");
                    sb.Append(kvp.Value.ToString());
                    sb.Append(" ]");
                    sw.WriteLine(sb);
                    sw.Flush();
                }
                sw.Close();
            }
        }
    }
}
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Read file, write file
« Reply #22 on: February 08, 2008, 02:06:25 PM »
Next question, I see that the Dictionary has key/value pairs.  What I need is triplets.   Is this possible?  Where key would be the Character ie "A", Value would be Character.Count - 3, and the 3rd value would be our Stores Number (part number in warehouse) ie 5121802
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Read file, write file
« Reply #23 on: February 08, 2008, 02:21:54 PM »
Next question, I see that the Dictionary has key/value pairs.  What I need is triplets.   Is this possible?  Where key would be the Character ie "A", Value would be Character.Count - 3, and the 3rd value would be our Stores Number (part number in warehouse) ie 5121802
I don't think so, but you maybe be able to do it.  You could define the key as a List or ArrayList, and then just add one to the first (or second) value in the List, but that is just thinking out loud.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Read file, write file
« Reply #24 on: February 08, 2008, 04:04:56 PM »
I have another question that is perplexing me.  I wrote this using .Net3.5, and it works on a machine w/ .Net3.0.  Is this b/c my project is so simple the form stuff hasn't changed in 3.5?  Also, using Express, how can I make a deployable exe for distribution?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Chuck Gabriel

  • Guest
Re: Read file, write file
« Reply #25 on: February 08, 2008, 04:23:59 PM »
Next question, I see that the Dictionary has key/value pairs.  What I need is triplets.   Is this possible?  Where key would be the Character ie "A", Value would be Character.Count - 3, and the 3rd value would be our Stores Number (part number in warehouse) ie 5121802

Could you declare your container something like this:

Dictionary<char, KeyValuePair<int, int> > mrDuhsContainer = new Dictionary<char, KeyValuePair<int, int> >;

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Read file, write file
« Reply #26 on: February 08, 2008, 05:02:48 PM »
I have another question that is perplexing me.  I wrote this using .Net3.5, and it works on a machine w/ .Net3.0.  Is this b/c my project is so simple the form stuff hasn't changed in 3.5?
Yes.

Also, using Express, how can I make a deployable exe for distribution?
Don't know since I use SharpDevelop, and I only code for myself really.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Read file, write file
« Reply #27 on: February 08, 2008, 05:30:07 PM »
Next question, I see that the Dictionary has key/value pairs.  What I need is triplets.   Is this possible?  Where key would be the Character ie "A", Value would be Character.Count - 3, and the 3rd value would be our Stores Number (part number in warehouse) ie 5121802

Could you declare your container something like this:

Dictionary<char, KeyValuePair<int, int> > mrDuhsContainer = new Dictionary<char, KeyValuePair<int, int> >;
Sounds good to me, will have try that on monday
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Read file, write file
« Reply #28 on: February 08, 2008, 05:30:32 PM »
I have another question that is perplexing me.  I wrote this using .Net3.5, and it works on a machine w/ .Net3.0.  Is this b/c my project is so simple the form stuff hasn't changed in 3.5?
Yes.

thought so
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Read file, write file
« Reply #29 on: February 08, 2008, 07:43:50 PM »
David,
I'd handle the initialisation of the Dictionary slightly differently ; with a mind to making modifications more efficient without needing to recompile the code.

If you were to have a definition file that contained {say} one entry per item for ;

char , catalogCode

ie
a 101234583
~ 102333356
etc ..

read this file in and initialise the Dictionary , adding a zero count for each.

you could then replace the casting of chars and the testing of the resultant int value
with a simple 
                        if (Dict.ContainsKey(chr))
                            Dict[chr] = ++Dict[chr];  //<<==== or whatever :-)

being secure in the knowledge that ONLY the characters you want to count will be already in the Dictionary.
 
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.