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

0 Members and 1 Guest are viewing this topic.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Read file, write file
« Reply #30 on: February 09, 2008, 10:35:19 AM »
Thanks Kerry.  I was thinking that, but hadn't figured out how I wanted to deal with it yet.  Are you saying that "a 10123
4583" would be ghe first value in the Dict, and the Count would be the second value?  I can see how that would work.  Ill have a go at it in a few minutes
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 #31 on: February 09, 2008, 06:36:26 PM »

Hi David,
I actually meant to use the first element as the key and the remaining elements as a List for the value.
.... but .. I've had second thoughts, cause the efficiency will depend on the quantum of hits the Dictionary is taking to update the value ( ie: how big is your text file ) and on what you will do with the data when it's collected.

.. though I still think initialising the dictionary from an external source is preferable.

It may be better to just collect the data in the simple dictionary then do the collation with the catalog numbers as a separate process.
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.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Read file, write file
« Reply #32 on: February 09, 2008, 08:03:13 PM »
It may be better to just collect the data in the simple dictionary then do the collation with the catalog numbers as a separate process.
I was thinking this, as my list of characters is very small, 40 at most (alpha, numeric, ().-).  Maybe the ' if needed, but still, very small list.  And, per substation, there will be a very few high quanity letters, and others that have a small quanity.  Most characters wont have any at all.
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 #33 on: February 09, 2008, 10:05:06 PM »
40 characters?  You're gonna need a bigger alphabet. :-)

Glenn R

  • Guest
Re: Read file, write file
« Reply #34 on: February 10, 2008, 05:52:23 AM »
Code: [Select]
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace CountChars {

class Program {

static void Main(string[] args) {

string fullFile = null;

using (TextReader tr = File.OpenText(@"C:\Temp\CountCharsTest.txt"))
fullFile = tr.ReadToEnd();

SortedDictionary<char, int> charDict = new SortedDictionary<char, int>();

foreach (char c in fullFile) {
if (!char.IsLetter(c))
continue;
if (charDict.ContainsKey(c))
charDict[c]++;
else
charDict.Add(c, 1);
}

foreach (KeyValuePair<char, int> kvp in charDict)
Console.WriteLine("Character: {0}\tCount: {1}", kvp.Key, kvp.Value);

Console.ReadLine();
}
}
}

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Read file, write file
« Reply #35 on: February 10, 2008, 05:36:12 PM »
40 characters?  You're gonna need a bigger alphabet. :-)
We only use Capital letters in our signs, so I was thinking 26 letters, 10 numbers and a few punctionation marks would give me about 40 characters.
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 #36 on: February 10, 2008, 05:38:54 PM »
Glenn, I see you used a TextReader instead of a Streamreader.  Can I ask why and what benefits one has over the other?
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 #37 on: February 10, 2008, 05:44:25 PM »
40 characters?  You're gonna need a bigger alphabet. :-)
We only use Capital letters in our signs, so I was thinking 26 letters, 10 numbers and a few punctionation marks would give me about 40 characters.

I figured it was something like that, but I couldn't stop myself from trying to be witty.  :-)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Read file, write file
« Reply #38 on: February 11, 2008, 12:48:12 AM »
Last think I should embark on whilest under the influence of a sleeping pill is something like this, never mind sober, but I couldn't stop myself.

It's been a long time since I did any C# so I said "Time to re-hone those atrophying skills", ergo an attempt here.

I went the easy way of buffered binary read, thinking it would be faster (since we're ultimately dealing with binary data why convert to and from), and more bomb proof on large files.

Processes a 50 MB file pretty fast, maybe 1 second?

Anyway, sewerage --

Code: [Select]
using System;
using System.IO;

namespace Thetan
{
    class Program
    {
        private const int BUFFER_SIZE = 8192;

        private static long[] IndexBytes( string filename )
        {
            //  caller's responsibility to
            //  pass a valid, readable file
           
            long[] result = new long[ 256 ];

            //  use buffered binary read. Should be fast
            //  and won't chuck a wobbly on big files

            Stream inputStream            = File.OpenRead( filename );
            BufferedStream bufferedStream = new BufferedStream( inputStream );
            byte[] buffer                 = new byte[ BUFFER_SIZE ];
            int bytesRead;

            while ( ( bytesRead = bufferedStream.Read( buffer, 0, BUFFER_SIZE )) > 0 )
                for ( int i = 0 ; i < bytesRead ; i++ )
                    result[ buffer[ i ] ]++;
           
            bufferedStream.Close();

            return result;

        } //------------------------------------------------------------

        static void Main(string[] args)
        {
            //  This file example file is about 53 MB.
           
            string filename = @"C:\Docs\Downloads\iTunesSetup.exe";
           
            if ( File.Exists( filename ) )
            {
                long[] results = IndexBytes( filename);               
                ShowResults ( results, false );
            }

        } //------------------------------------------------------------
       
        private static void ShowResults ( long[] results, bool toUpper )
        {
            if ( toUpper ) // force upper case registers
                           // to represent lower + upper
            {   
                for ( int i = 65, k = 97 ; i < 91 ; i++, k++ )
                {   
                    results[i] += results[k];
                    results[k]  = 0;
                }   
            }         
           
            Console.WriteLine( "Talley" );           
            Console.WriteLine( "=============" );
           
            ShowRange ( results, 48, 58 );
            if ( !toUpper ) ShowRange ( results, 97, 123 );
            ShowRange ( results, 65, 91 );
           
            Console.WriteLine( "\nPress [Enter] to continue" );
            Console.ReadLine();
           
        } //------------------------------------------------------------

        private static void ShowRange ( long[] results, int startIndex, int endIndex )
        {
            for ( int i = startIndex ; i < endIndex ; i++ )
            {
                Console.WriteLine( "{0}'s => {1}", (char)i, results[i] );
            }
        }
    }
}

Looks verbose but I'm guessing it performs well against the other versions (tho I never benched anything myself).
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst