Code Red > .NET

Read file, write file

(1/8) > >>

David Hall:
ok, I'm sure there is a MUCH better way to do this, but I'm trying to bang this out real fast.  Im opening a file, and parsing character by character and counting how many "A's", "B's", etc.  Eventually I will write this out to a file, but I'm interested if anyone has a better way to parse this information.

--- Code: ---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)
        {
            int intA = 0;
            int intB = 0;
            int intC = 0;
            int intD = 0;
            int intE = 0;
            int intF = 0;
            int intG = 0;
            int intH = 0;
            int iniI = 0;
            int intJ = 0;
            int intK = 0;
            int intL = 0;
            int intM = 0;
            int intN = 0;
            int intO = 0;
            int intP = 0;
            int intQ = 0;
            int intR = 0;
            int intS = 0;
            int intT = 0;
            int intu = 0;
            int intV = 0;
            int intW = 0;
            int intX = 0;
            int intY = 0;
            int intZ = 0;
            int int1 = 0;
            int int2 = 0;
            int int3 = 0;
            int int4 = 0;
            int int5 = 0;
            int int6 = 0;
            int int7 = 0;
            int int8 = 0;
            int int9 = 0;
            int int0 = 0;

            using (StreamReader sr = new StreamReader("c:/TestFile.txt"))
            {

                string currentChar;
                while ((currentChar = sr.Read().ToString()) != "-1")
                {
                    switch (currentChar)
                    {
                        case "65":
                            intA++;
                            break;
                        case "66":
                            intB++;
                            break;
                        case "67":
                            intC++;
                            break;
                        case "68":
                            intD++;
                            break;
                        case "69":
                            intE++;
                            break;
                        case "70":
                            intF++;
                            break;
                        case "71":
                            intG++;
                            break;
                        case "72":
                            intH++;
                            break;
                        case "73":
                            intI++;
                            break;
                        case "74":
                            intJ++;
                            break;
                        case "75":
                            intK++;
                            break;
                        case "76":
                            intL++;
                            break;
                        case "77":
                            intM++;
                            break;
                        case "78":
                            intN++;
                            break;
                        case "79":
                            intO++;
                            break;
                        case "80":
                            intP++;
                            break;
                        case "81":
                            intQ++;
                            break;
                        case "82":
                            intR++;
                            break;
                        case 83:
                        case 84:
                        case 85:
                        case 86:
                        case 87:
                        case 88:
                        case 89:
                        case 90:
                        case 91:
                        case 92:
                        case 93:
                        case 94:
                        case 95:
                        case 96:
                        case 97:
                        case 98:
                        case 99:
                        case 100:

                        default:
                            break;
                    }
                }

            }
            Console.WriteLine("A=" + intA.ToString());
            Console.WriteLine("B=" + intB.ToString());
        }
    }
}
--- End code ---

David Hall:
As you can see from the code, I quit writing at about 82, looking to you guys for pointers.

MickD:
Perhaps you can use a map/dictionary (yes I like those :) ), create the 'keys' with the int values using a loop and store the 'A++' in the 'value' of the key.
As you read in the keys, add one to the value in a loop, something like (pseudo code) -

// creating the dictionary/map
Dict dict = new Dict();
// create the records:
For (i=0; i<36; i++) //amount of vals
    dict.addRecord(key = 65+i.asString() , value = 0);
end

// reading and storing:
while(not end of file)
 read in a char
    dict.getAt(asAsciiNum(char)) += 1;
end while
   
hth.

PS. I'd write an example for you David but it would take me longer to set up my c# ide and study the api than it would to code it :laugh:

David Hall:
OK, most of that went over my head, but I get the gist. :-)  I will look up Dict's tomorrow at work.

David Hall:
Other than that, do you think Im on track for parsing the file?

Navigation

[0] Message Index

[#] Next page

Go to full version