[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

reading data from txt file

Posted on 2006-04-23
8
Medium Priority
?
235 Views
Last Modified: 2010-04-16
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 ...
0
Comment
Question by:Tech_Men
  • 3
  • 3
  • 2
8 Comments
 
LVL 44

Expert Comment

by:bruintje
ID: 16520574
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
 

Author Comment

by:Tech_Men
ID: 16520595
how can i use the delimter and where in the code ?
0
 
LVL 44

Expert Comment

by:bruintje
ID: 16520714
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16520803
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
 
LVL 44

Expert Comment

by:bruintje
ID: 16520828
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
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 16520842
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
 

Author Comment

by:Tech_Men
ID: 16524690
thanks !!!
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16525115
Glad I was able to help. :=)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month20 days, 12 hours left to enroll

865 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question