Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi All
I am working on C# windows application where I read enter text file and enter to a array variable now I am getting 10 lines in the array. So up to this it is Fine. But
Now I have to separate each field by tab key and enter to a hash table all the 10 lines with each field (There is five field).
can any body help me how to do this please

Below is my code Please Check
C#
private void btnReadFile_Click(object sender, EventArgs e)
{
   string[] lines = File.ReadAllLines(txtFilePath.Text); // Here I got all the 10 lines of the file
   Hashtable ht = new Hashtable();
   foreach(string line in lines)
   {
     //here I want to enter every line in the hash table with each column
     //Is it possible? if it is possible please help
   }  
}
Posted
Updated 12-Mar-12 21:11pm
v5
Comments
Sergey Alexandrovich Kryukov 12-Mar-12 2:53am    
Not clear. Help with what? What's the problem?
--SA
IndrajitDasgupat 13-Mar-12 1:58am    
Here I got the all the array string of the text file Now I want to put every line in the hash table
Sergey Alexandrovich Kryukov 14-Mar-12 2:36am    
Well, what blocks you from doing it? By the way, use generic collection instead of obsolete HashTable.
I think you already got all the answers you need.
--SA
nagendrathecoder 12-Mar-12 3:26am    
Can you show us some code of reading file or structure of file and array.
IndrajitDasgupat 13-Mar-12 1:59am    
I put the code please check

OK let me try t give you a solution, bit lengthy but it could work. [NOTE: all pseudocode, only logic no syntactic accuracy]

have a class/struct to hold the tab separated data and a function inside to do the split

C#
class columcollecton
{
string0;
string1;
string2;

public void SplitAndPopulate(string line)
{
   string[] str = line.split('\t');
   string0 = str[0];
   string1 = str[1];
   ...
   ...
}

}


now in you original code
C#
private void btnReadFile_Click(object sender, EventArgs e)
{
   string[] lines = File.ReadAllLines(txtFilePath.Text); //
   Hashtable ht = new Hashtable();
   foreach(string line in lines)
   {
     //create columncollection object
     //call the SplitAndPopulate
     //add the custom struct you have in hastable, internally the struct contains tab separated strings
     //NOTE: I think it would be better if you use a Dictionary instead of hashtable.
   }  
}
 
Share this answer
 
Comments
nagendrathecoder 13-Mar-12 3:18am    
Nice answer.
I suspect you are using static methods provided by the File class to read the files. I would suggest using Stream as that is better if used wisely.

For your question, there is a method called Split in the String class. Look it up.
 
Share this answer
 
You can split each line of array on tab character using
String.Split()
method.
After splitting, it'll create create another string array containing column information.
After that you can either use this new array or add all elements of this array to a Hashtable and use it.

Please let me know if you need something else.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900