Author Topic: [C#] line Checker  (Read 2227 times)

0 Members and 1 Guest are viewing this topic.

Offline Huntondoom

  • Baron
  • ****
  • Posts: 856
  • Cookies: 17
  • Visual C# programmer
    • View Profile
[C#] line Checker
« on: June 20, 2011, 11:46:33 pm »
As a practice I made a program that deletes all double lines in a file


Code: (C#) [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace ConsoleApplication1
{
    class Program
    {
 
        #region "processes "


        static void Main(string[] args)
        {


            if (args.Length > 0)
            {
                for (int I = 0; I < args.Length; I++)
                {
                    if (File.Exists(args[I]))
                    {
                        CheckFileLines(args[I]);
                    }
                }


            }
            else
            {
            FileInvalid:
                Console.WriteLine("Please Give Path to File To Check");
                string Path = ConvertPath(Console.ReadLine());
                if (!File.Exists(Path)) { goto FileInvalid; }
                CheckFileLines(Path);
            }
            Console.ReadLine();
        }


        static void CheckFileLines(string Path)
        {
            List<string> TextLines = new List<string>();
            List<string> LinesTNF = new List<string>();
            StreamReader Reader;


            Console.WriteLine(Path);
            try
            {
                Reader = new StreamReader(File.OpenRead(Path));
                while (!Reader.EndOfStream)
                {
                    TextLines.Add(Reader.ReadLine());                       
                }
                Reader.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("");
                Console.WriteLine(ex.Message);
            }
            for (int A = 0; A < TextLines.Count; A++)
            {
                bool isin = false;
                for (int B = 0; B < LinesTNF.Count; B++)
                {
                    if (LinesTNF[B] == TextLines[A]) { isin = true; }
                }
                if (!isin) { LinesTNF.Add(TextLines[A]); }
            }
            Console.WriteLine("Lines Deleted: " + (TextLines.Count - LinesTNF.Count).ToString());


            File.WriteAllText(Path, string.Join(Environment.NewLine, LinesTNF.ToArray()));
        }


        #endregion


        #region "Functions "


        static string ConvertPath(string Path)
        {
            Path = Path.Replace("%DOCUMENTS%", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
            return Path;
        }


        #endregion
    }
}
Aslong as you are connected to the internet, you'll have no privacy

Advanced Internet Search
Clean Up!

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: [C#] line Checker
« Reply #1 on: June 21, 2011, 10:49:10 am »
Hmmm....
Code: [Select]
List := TStringList.Create;
List := dupIgnore;
List.LoadFromFile(FILE);
List.WriteToFile(FILE);

And never ever never ever ever never use "GoTo" ( { goto FileInvalid; } ). It's a bad practice. You may use it on rare occasions or when there is no other way out. You could have just made a new procedure for FileInvilid and used that, since it's an app anyway...

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: [C#] line Checker
« Reply #2 on: June 21, 2011, 11:24:27 am »
Code: [Select]
else
            {
            FileInvalid:
                Console.WriteLine("Please Give Path to File To Check");
                string Path = ConvertPath(Console.ReadLine());
                if (!File.Exists(Path)) { goto FileInvalid; }
                CheckFileLines(Path);
            }
This is just a primitive loop,  change it to a while loop, like while file.exists != true { do that}
~Factionwars

Offline Huntondoom

  • Baron
  • ****
  • Posts: 856
  • Cookies: 17
  • Visual C# programmer
    • View Profile
Re: [C#] line Checker
« Reply #3 on: June 21, 2011, 11:36:43 am »
sorry I used to my Calculator where Goto is a primary source of loop :P
Aslong as you are connected to the internet, you'll have no privacy

Advanced Internet Search
Clean Up!

Offline xzid

  • Knight
  • **
  • Posts: 329
  • Cookies: 41
    • View Profile
Re: [C#] line Checker
« Reply #4 on: July 26, 2011, 02:54:30 am »
Code: [Select]
$h = @{}; gc file.txt | % { if(!$h[$_]) { $_; $h[$_] = 1 } }
I love this -> % { }, Powershell rocks!

I'd recommend using C# hashtables as well, would simplify the code(the dual for() loop part).