Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem reading-in data from text file

Status
Not open for further replies.

jasonsalas

IS-IT--Management
Jun 20, 2001
480
GU
Hi everyone,

I've been wondering why this isn't working: I was curious and wanted to rebuild the old MWSC.NextLink control that shiiped with ASP 3.0 for ASP.NET.

Essentially, I'm trying to read from a tab-delimited text file (2 columns) and have the values before and after the tab be added to a Hashtable. But for some reason, I eep drawing an ArgumentOutOfRangeException when trying to run the code. Specifically, I get "Length cannot be less than zero. Parameter name: length", indicating that no data's being read-in.

private Hashtable GetFileList(string file)
{
Hashtable ht = new Hashtable();
StreamReader sr = File.OpenText(file);

// TODO: somewhere in here the code is going wrong...it works otherwise....
while(sr.Peek() > -1)
{
string contents = sr.ReadLine();
tabPosition = contents.IndexOf("\n",0);
before = contents.Substring(0,tabPosition); // the Hashtable's key
after = contents.Substring(tabPosition+1); // the Hashtable's value
ht.Add(before,after);
}

sr.Close();

return ht;
}

Any ideas?

Thanks!

Jas
 
contents.IndexOf("\n",0);

gives you the position of the endline, not the position of the tab...

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Thanks...I changed it to

contents.IndexOf("\t",0);

...and now it works! It wasn;t doing this earlier so in my debugging stupor I must've forgotten to change it back. Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top