Need Help In Reading A Text File
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Need Help In Reading A Text File

  1. #1
    Join Date
    Mar 2011
    Posts
    2

    Resolved Need Help In Reading A Text File

    Hello,

    I need help in reading the attributes in the attached text file. I am taking values from 8 textboxes as inputs and i then want to compare those textbox values with the attributes in the text file and then want to generate decision based on them. 0 and 1 represent decision.

    Kindly find the attached file
    Attached Files Attached Files

  2. #2
    Join Date
    Mar 2011
    Posts
    2

    Re: Need Help In Reading A Text File

    Quote Originally Posted by waqasnu View Post
    Hello,

    I need help in reading the attributes in the attached text file. I am taking values from 8 textboxes as inputs and i then want to compare those textbox values with the attributes in the text file and then want to generate decision based on them. 0 and 1 represent decision.

    Kindly find the attached file
    Sorry wrong file attached. here is the correct file.
    Attached Files Attached Files

  3. #3
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: Need Help In Reading A Text File

    What exactly do you mean by "Attributes" ?
    Taking a guess, you need to:
    Code:
    string[] lines = File.ReadAllLines(filePath);
    foreach (string line in lines)
    {
        string[] splitLine = line.Split(':');
        // followed by more splitting etc
        // to get each
        // attribute (glucose_tol)
        // operator (<=)
        // operand (127)
        // result (0)
    
        // make a class/struct out of the above
    }
    I think you then need to build some sort of (decision) Tree structure, where each node is a class/struct from above, with glucose_tol at the root....

    You should then be able to write a routine to take values for attributes and process them, through your decision tree to obtain a diagnosis (result).

    Code:
    glucose_tol <= 127 : 0
       mass_index <= 26.4 : 1
          times_pregnant <= 7 : 0 
          times_pregnant > 7 :
             mass_index <= 0 : 1 
             mass_index > 0 : 0 
       mass_index > 26.4 :
          age <= 28 : 0 
          age > 28 :
             glucose_tol <= 99 : 0 
             glucose_tol > 99 :
                pedigree <= 0.561 : 0 
                pedigree > 0.561 :
                   times_pregnant > 6 : 1 
                   times_pregnant <= 6 :
                      age <= 30 : 1 
                      age > 30 :
                         age <= 34 : 0 
                         age > 34 :
                            mass_index <= 33.1 : 1 
                            mass_index > 33.1 : 0 
    glucose_tol > 127 :
       mass_index <= 29.9 :
          glucose_tol <= 145 : 0 
          glucose_tol > 145 :
             age <= 25 : 0 
             age > 25 :
                age > 61 : 0 
                age <= 61 :
                   mass_index <= 27 : 1 
                   mass_index > 27 :
                      diastolic_pb > 82 : 0 
                      diastolic_pb <= 82 :
                         pedigree <= 0.396 : 1 
                         pedigree > 0.396 : 0 
       mass_index > 29.9 :
          glucose_tol > 157 : 1 
          glucose_tol <= 157 :
             diastolic_pb <= 61 : 1 
             diastolic_pb > 61 :
                age <= 30 : 0 
                age > 30 : 1
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  4. #4
    Join Date
    Mar 2011
    Location
    London
    Posts
    54

    Re: Need Help In Reading A Text File

    I recommend you use a regular expression for the parsing of this file. It's fast and gives you good control.

    Here is some code that will parse your file and pull out the relevant parts. I'm not sure what you mean by attributes so I pull out any of the useful stuff and leave it to you to choose what you want:

    Code:
    using System;
    using System.IO;
    using System.Text.RegularExpressions;
    
    namespace RedBully
    {
        class Program
        {
            /// <summary>
            /// Regular expression for parsing OutSave.txt
            /// </summary>
            private const string REG_EX = @"^\s*(\w+)\s([<=>]{1,2})\s([\d\.]+).\s\:\s([01])\s*$";
    
            static void Main(string[] args)
            {
                //Read in the file
                StreamReader sr = File.OpenText("outSave.txt");
                string data = sr.ReadToEnd();
    
                //Create Regular Expression to parse the data
                Regex regEx = new Regex(REG_EX, RegexOptions.Multiline);
    
                //Find each match on each line
                Match match = regEx.Match(data);
                do
                {
                    //Output the different parts of each line to console.
                    //***You can access the data you need using the groups and use them as you like
                    Console.WriteLine("Attribute: " + match.Groups[1].ToString());
                    Console.WriteLine("Operator: " + match.Groups[2].ToString());
                    Console.WriteLine("Value: " + match.Groups[3].ToString());
                    Console.WriteLine("0 or 1: " + match.Groups[4].ToString());
                    Console.WriteLine("-----");
    
                    match = match.NextMatch();
                }
                while (match.Success);
                
    
                //Puase before end of program
                Console.WriteLine("Press a key to continue...");
                Console.ReadKey();
    
            }
        }
    }

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center