Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Hashtable reading text file???

Hashtable reading text file???

Scheduled Pinned Locked Moved C#
comhelptutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gman44
    wrote on last edited by
    #1

    I've searched all my books and the Internet and cannot find a good example to read a text file into a hashtable. My text file is tab delimited as such below)and I need to load them into a hashtable as a DNS cashe...... Help......... 123.123.123.123 www.mywebsite.com 111.222.333.444 www.nowebsite.com

    N 1 Reply Last reply
    0
    • G gman44

      I've searched all my books and the Internet and cannot find a good example to read a text file into a hashtable. My text file is tab delimited as such below)and I need to load them into a hashtable as a DNS cashe...... Help......... 123.123.123.123 www.mywebsite.com 111.222.333.444 www.nowebsite.com

      N Offline
      N Offline
      Nick Parker
      wrote on last edited by
      #2

      kvnsdr wrote: I've searched all my books and the Internet and cannot find a good example to read a text file into a hashtable. Books typically cover how to use particular mechanisms, you simply need to put the pieces together. This is a quick example method I wrote to read your file and insert the data into the Hashtable. Hope this helps.

      	private Hashtable GetHashtableFromFile(string file)
      	{
      		Hashtable ht = new Hashtable();
      		string line = string.Empty;
      		using(StreamReader reader = new StreamReader(file))
      		{	
      			while((line = reader.ReadLine()) != null)
      			{
      				string\[\] args = line.Split(new char\[\]{' '});
      				ht.Add(args\[0\], args\[1\]);	
      				Console.WriteLine("IP:{0}, Site:{1}", args\[0\], args\[1\]);
      			}
      		}
      		return ht;
      	}
      

      - Nick Parker
      My Blog | My Articles

      G 1 Reply Last reply
      0
      • N Nick Parker

        kvnsdr wrote: I've searched all my books and the Internet and cannot find a good example to read a text file into a hashtable. Books typically cover how to use particular mechanisms, you simply need to put the pieces together. This is a quick example method I wrote to read your file and insert the data into the Hashtable. Hope this helps.

        	private Hashtable GetHashtableFromFile(string file)
        	{
        		Hashtable ht = new Hashtable();
        		string line = string.Empty;
        		using(StreamReader reader = new StreamReader(file))
        		{	
        			while((line = reader.ReadLine()) != null)
        			{
        				string\[\] args = line.Split(new char\[\]{' '});
        				ht.Add(args\[0\], args\[1\]);	
        				Console.WriteLine("IP:{0}, Site:{1}", args\[0\], args\[1\]);
        			}
        		}
        		return ht;
        	}
        

        - Nick Parker
        My Blog | My Articles

        G Offline
        G Offline
        gman44
        wrote on last edited by
        #3

        I like the coding. I have an error in my code that says: "Index was outside the bounds of the array" All I changed was using(StreamReader reader = new StreamReader(MyFile.txt")); ????????????????

        K 1 Reply Last reply
        0
        • G gman44

          I like the coding. I have an error in my code that says: "Index was outside the bounds of the array" All I changed was using(StreamReader reader = new StreamReader(MyFile.txt")); ????????????????

          K Offline
          K Offline
          Kika_
          wrote on last edited by
          #4

          I think you have incorrect format of input file. Somewhere there are empty strings or parts of string are not delimited with tab symbol modify the code like this private Hashtable GetHashtableFromFile(string file) { Hashtable ht = new Hashtable(); string line = string.Empty; using(StreamReader reader = new StreamReader(file)) { int i = 0; while((line = reader.ReadLine()) != null) { i++; string[] args = line.Split(new char[]{' '}); if (args.Length != 2) { Console.WriteLine("Invalid input string: " + i.ToString()); continue; } ht.Add(args[0], args[1]); Console.WriteLine("IP:{0}, Site:{1}", args[0], args[1]); } } return ht; } Invalid string will be skiped and appropriate message displayed

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups