Angelia
Angelia

Reputation: 1

C# object oriented read text file

I read the text files in the folder, I get the absolute value of each column, and I showed the maximum value and the name of the file in the datagrid. But I need to write my methods in a class and call it in form.cs, not in form.cs. Can you help me? my code is like this:

DirectoryInfo info = new DirectoryInfo(@"D:\Desktop\..");
FileInfo[] Files = info.GetFiles("*.txt"); 
List<string> list = new List<string>();
List<Double> values= new List<Double>();

foreach (FileInfo file in Files)
    {
         string name = info.Name;

         list.Add(file.Name);
         string[] lines = System.IO.File.ReadAllLines(@"D:\Desktop\..\" + file, Encoding.GetEncoding("windows-1254")); 

         values.AddRange(MultiColumns(lines));

    }

private List<Double> MultiColumns(String[] strs)
    {
        double col1Max = 0; 
        double col2Max = 0;
        double col3Max = 0;
        var list = new List<Double>();
        var format = new NumberFormatInfo();
        format.NegativeSign = "-";
        format.NumberNegativePattern = 1;
        format.NumberDecimalSeparator = ".";

        foreach (var row in strs)
        {
            var rowElements = row.Split(',');


            Double temp1 = 0;
            Double temp2 = 0;
            Double temp3 = 0;

            Double.TryParse(rowElements[0], NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, format, out temp1);
            Double.TryParse(rowElements[1], NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, format, out temp2);
            Double.TryParse(rowElements[2], NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, format, out temp3);


            col1Max = getMax(col1Max, temp1);
            col2Max = getMax(col2Max, temp2);
            col3Max = getMax(col3Max, temp3);

        }

        list.Add(col1Max);
        list.Add(col2Max);
        list.Add(col3Max);
        return list;
    }

    private double getMax(double colMax, double temp)
    {

        //Math.Abs(colMax);
        if (temp < 0)
        {
            temp *= -1;
        }
        if (temp > colMax)
        {
            colMax = temp;
        }
        return colMax;
    }

Upvotes: 0

Views: 518

Answers (2)

jdweng
jdweng

Reputation: 34433

Try following :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Globalization;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string folder = @"D:\Desktop\";
            ReadTextFile readTextFile = new ReadTextFile(folder);
            List<double> absolute = readTextFile.values.Select(x => Math.Abs(x)).ToList();
            double max = readTextFile.values.OrderByDescending(x => x).FirstOrDefault();
            dataGridView1.DataSource = readTextFile.dt;
        }


    }

    public class ReadTextFile
    {
        public List<string> list { get; set; }
        public List<Double> values { get; set; }
        public DataTable dt { get; set; }
        public ReadTextFile(string folder)
        {
            DirectoryInfo info = new DirectoryInfo(folder);
            FileInfo[] Files = info.GetFiles("*.txt");
            list = new List<string>();
            values = new List<Double>();
            dt = new DataTable();
            dt.Columns.Add("max1", typeof(double));
            dt.Columns.Add("max2", typeof(double));
            dt.Columns.Add("max3", typeof(double));

            NumberFormatInfo format = new NumberFormatInfo();
            format.NegativeSign = "-";
            format.NumberNegativePattern = 1;
            format.NumberDecimalSeparator = ".";


            foreach (FileInfo file in Files)
            {

                string name = info.Name;


                list.Add(file.Name);
                string[] lines = System.IO.File.ReadAllLines(folder + file,
                                                            Encoding.GetEncoding("windows-1254"));
                try
                {
                    values.AddRange(lines.Skip(1).SelectMany(x => x.Split(',').Take(3).Select(y => Double.Parse(y, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, format))));
                    for(int row = 1; row < lines.Length; row++)
                    {
                        dt.Rows.Add(lines[row].Split(',').Take(3).Select(y => Double.Parse(y, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, format)));
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Bad data in file : '{0}'", file.Name);
                }
            }
        }

    }
}

Upvotes: 0

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34180

You can simply do all these with Linq as one-liner:

var results = Directory.GetFiles(@"D:\Desktop\..","*.txt")
   .Select(file => new { file, max = File.ReadAllText(file).Split(',')
   .Select(x=> double.Parse(x.Trim())).Max()});

If you want to also get the maximum of all max values:

allMax = result.Max(x => x.max);

to Test:

foreach(var item in results)
   Console.WriteLine($"file: {item.file} - max: {item.max}");

Upvotes: 1

Related Questions