Link to home
Start Free TrialLog in
Avatar of Tech_Men
Tech_MenFlag for Israel

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 ...
Avatar of Brian Mulder
Brian Mulder
Flag of Netherlands image

Hello Tech_Men,

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
Avatar of Tech_Men

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.ToString());
                }
            }
        }
 
this will first split the file on linefeed '/n'
then each line on a tab character '/t'
Avatar of Fernando Soto
Hi Tech_Men;

Here is a solution using Regular Expression.

Imports System.IO
Imports System.Text.RegularExpressions

        Dim sr As New StreamReader("C:\Temp\ParseNums.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").Value)

            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(mystringarray[0]);
                MessageBox.Show(mystringarray[1]);                
            }
        }
    }
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks !!!
Glad I was able to help. :=)