Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Hashtable read text file???

Posted on 2004-08-23
6
Medium Priority
?
249 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:kvnsdr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11876438

    static Hashtable ReadFromFile(string filename)
    {
    StreamReader SR;
    string S;
    Hashtable ret = new Hashtable
    SR=File.OpenText(filename);
    S=SR.ReadLine();
    while(S!=null)
    {
         string [] tmp = S.Split("\t");
         if(tmp.Count == 2) {
              if(!ret.Contains(tmp[0]) {
                    ret.add(tmp[0], tmp[1])
              } else {
                    how do you want to handle duplicate entries ?
              }
         } else {
              throw new system.exception("bad data");
         }
    S=SR.ReadLine();
    }
    SR.Close();
    }
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11876445
btw this will most likely require a little editing no compiler here this sec so just typed here.
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 11876915

  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 SR = File.OpenText("MyFile.txt");  

????????????????
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Accepted Solution

by:
somnatic earned 2000 total points
ID: 11878436
i hope gregoryyoung won't kill me for editing his code ;)
try this .. works for me .. and .. don't forget the using System.IO; Statement



            private void button1_Click(object sender, System.EventArgs e)
            {
                  Hashtable r = ReadFromFile("d:\\testtab.txt");
            }
            static Hashtable ReadFromFile(string filename)
            {
                  StreamReader SR;
                  string S;
                  Hashtable ret = new Hashtable();
                  SR=File.OpenText(filename);
                  S=SR.ReadLine();
                  while(S!=null)
                  {
                        string [] tmp = S.Split('\t');
                        if(tmp.Length == 2)
                        {
                              if(!ret.Contains(tmp[0]) )
                              {
                                    ret.Add(tmp[0], tmp[1]);
                              }
                              else
                              {
                              }
                        }
                        else
                        {
                              throw new System.Exception("bad data");
                        }
                        S=SR.ReadLine();
                  }
                  SR.Close();

                  return ret;
            }
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11888402
jeeze forget a return statement typing without a compiler and lose all credit ... nice one.
0
 
LVL 4

Expert Comment

by:somnatic
ID: 11889478
you forgot the semicolon somewhere at the beginning ;) ..

any way to shift points ?
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question