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 ...
Tech_MenAsked:
Who is Participating?
 
Fernando SotoRetiredCommented:
Sorry Tech_Men posted last solution in the wrong language. Here it is in C#.

using System.IO;
using System.Text.RegularExpressions;

      StreamReader sr =new StreamReader(@"C:\Temp\ParseNums.txt");
      string input = sr.ReadToEnd();
      sr.Close();
      Regex re = new Regex(@"(?<ProdKey>[\d-]+)\s+(?<Quant>-\d+)",
            RegexOptions.Compiled);
      MatchCollection mc;

      mc = re.Matches(input);
      foreach( Match m in mc )
      {
            // Get the information from the Regex object
            string ProdKey = m.Groups["ProdKey"].Value;
            int QuantitySold = Convert.ToInt32(m.Groups["Quant"].Value);

            MessageBox.Show("Product Key = " + ProdKey + " Quantity Sold = " +
                  QuantitySold.ToString());
      }
0
 
bruintjeCommented:
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
0
 
Tech_MenAuthor Commented:
how can i use the delimter and where in the code ?
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
bruintjeCommented:
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'
0
 
Fernando SotoRetiredCommented:
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
0
 
bruintjeCommented:
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]);                
            }
        }
    }
0
 
Tech_MenAuthor Commented:
thanks !!!
0
 
Fernando SotoRetiredCommented:
Glad I was able to help. :=)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.