Tech_Men
asked on
reading data from txt file
hi there i have txt file that i reading from him
the txt file is like this :
150 -6
140 -10
150-5 -15
the first number is the product key and the -6 is the quantity that sold in day.
i need to poll out the proKey to a string variable and the quantity to a int variable
then i need to send them to a func
i tryd to do it like that :
string sLine="";
ArrayList arrText = new ArrayList();
char delimiter = ' ';
while (sLine != null)
{
sLine = objReader.ReadLine();
if (sLine != null)
arrText.Add(sLine);
}
objReader.Close();
foreach (string sOutput in arrText)
MessageBox.Show(sOutput);
but i am geeting the ProKey and the quntaty in the same line i need to split them into a 2 varible...
thanks ...
the txt file is like this :
150 -6
140 -10
150-5 -15
the first number is the product key and the -6 is the quantity that sold in day.
i need to poll out the proKey to a string variable and the quantity to a int variable
then i need to send them to a func
i tryd to do it like that :
string sLine="";
ArrayList arrText = new ArrayList();
char delimiter = ' ';
while (sLine != null)
{
sLine = objReader.ReadLine();
if (sLine != null)
arrText.Add(sLine);
}
objReader.Close();
foreach (string sOutput in arrText)
MessageBox.Show(sOutput);
but i am geeting the ProKey and the quntaty in the same line i need to split them into a 2 varible...
thanks ...
ASKER
how can i use the delimter and where in the code ?
sorry, thought you already had more code
private void button1_Click(object sender, EventArgs e)
{
StreamReader objReader = new StreamReader("c:\\" + @"\testFile.txt");
string str1;
object[] str2;
object[] str3;
str1 = objReader.ReadToEnd();
objReader.Close();
str2 = str1.Split('\n');
foreach (object v in str2)
{
str3 = v.ToString().Split('\t');
foreach (object v2 in str3)
{
MessageBox.Show(v2.ToStrin g());
}
}
}
this will first split the file on linefeed '/n'
then each line on a tab character '/t'
private void button1_Click(object sender, EventArgs e)
{
StreamReader objReader = new StreamReader("c:\\" + @"\testFile.txt");
string str1;
object[] str2;
object[] str3;
str1 = objReader.ReadToEnd();
objReader.Close();
str2 = str1.Split('\n');
foreach (object v in str2)
{
str3 = v.ToString().Split('\t');
foreach (object v2 in str3)
{
MessageBox.Show(v2.ToStrin
}
}
}
this will first split the file on linefeed '/n'
then each line on a tab character '/t'
Hi Tech_Men;
Here is a solution using Regular Expression.
Imports System.IO
Imports System.Text.RegularExpress ions
Dim sr As New StreamReader("C:\Temp\Pars eNums.txt" )
Dim input As String = sr.ReadToEnd()
sr.Close()
Dim re As New Regex("(?<ProdKey>[\d-]+)\ s+(?<Quant >-\d+)", _
RegexOptions.Compiled)
Dim mc As MatchCollection
mc = re.Matches(input)
For Each m As Match In mc
' Get the information from the Regex object
Dim ProdKey As String = m.Groups("ProdKey").Value
Dim quantitySold As Integer = CInt(m.Groups("Quant").Val ue)
MessageBox.Show("Product Key = " & ProdKey & " Quantity Sold = " & _
quantitySold.ToString())
Next
Let me know if you have any questions.
Fernando
Here is a solution using Regular Expression.
Imports System.IO
Imports System.Text.RegularExpress
Dim sr As New StreamReader("C:\Temp\Pars
Dim input As String = sr.ReadToEnd()
sr.Close()
Dim re As New Regex("(?<ProdKey>[\d-]+)\
RegexOptions.Compiled)
Dim mc As MatchCollection
mc = re.Matches(input)
For Each m As Match In mc
' Get the information from the Regex object
Dim ProdKey As String = m.Groups("ProdKey").Value
Dim quantitySold As Integer = CInt(m.Groups("Quant").Val
MessageBox.Show("Product Key = " & ProdKey & " Quantity Sold = " & _
quantitySold.ToString())
Next
Let me know if you have any questions.
Fernando
here is my latest on this one, ligths are going out :)
this will add all split lines as array to the arraylist and to see if that works i added a new string array at the end with 2 messageboxes
private void button1_Click(object sender, EventArgs e)
{
StreamReader objReader = new StreamReader("c:\\" + @"\testFile.txt");
string str1;
object[] str2;
string[] str3;
int i=0;
ArrayList arrText = new ArrayList();
str1 = objReader.ReadToEnd();
objReader.Close();
str2 = str1.Split('\n');
foreach (object v in str2)
{
str3 = v.ToString().Split('\t');
arrText.Add(str3);
string[] mystringarray = (string[])arrText[0];
MessageBox.Show(mystringar ray[0]);
MessageBox.Show(mystringar ray[1]);
}
}
}
this will add all split lines as array to the arraylist and to see if that works i added a new string array at the end with 2 messageboxes
private void button1_Click(object sender, EventArgs e)
{
StreamReader objReader = new StreamReader("c:\\" + @"\testFile.txt");
string str1;
object[] str2;
string[] str3;
int i=0;
ArrayList arrText = new ArrayList();
str1 = objReader.ReadToEnd();
objReader.Close();
str2 = str1.Split('\n');
foreach (object v in str2)
{
str3 = v.ToString().Split('\t');
arrText.Add(str3);
string[] mystringarray = (string[])arrText[0];
MessageBox.Show(mystringar
MessageBox.Show(mystringar
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks !!!
Glad I was able to help. :=)
those spaces you are looking for are probably tabs you could try it with
char delimiter = (char)Keys.Enter;
as the delimiter, and i miss the split on the delimiter too in this code snippet you posted but having the tab will at least correct the empty space i think
hope this helps a bit
bruintje