0

I am filling data from memory mapped file to string like :

AAPL,2013-1-2
Open:79.117
Close:78.433
High:79.286
Low:77.376
Volume:139948984

AAPL,2013-1-3
Open:78.268
Close:77.442
High:78.524
Low:77.286
Volume:88114464

and so on...

So now I want to make an array of close value of all days. And there are collection of thousands of days data in memory mapped file and string. So how can I fetch close value and can make array of its?

I am trying to make it's array but it's make whole data into single array. So it's not what i want.

string[] lines = System.IO.File.ReadAllLines(@"D:\mine.txt");
foreach (string line in lines)
{
    // Use a tab to indent each line of the file.
    Console.WriteLine("\t" + line);
}

byte[] bytes = new byte[10000000];
stream.ReadArray(0, bytes, 0, bytes.Length);
string txt = Encoding.UTF8.GetString(bytes).Trim('\0');`

So I need an array of all close value to fetch from that string. Like that:

{78.433, 77.442, etc..}
1
  • I'm not convinced I fully understand the question. However, if you are enumerating the lines of the file, and the file is formatted as you have indicated, inside your foreach loop, simply check if the the line starts with "Close" and if it does, parse the close value from it. Commented Jan 28, 2019 at 10:10

4 Answers 4

3

Try this:

decimal[] arrayOfCloses =
    File
        .ReadAllLines(@"D:\mine.txt")
        .Select(x => x.Split(':'))
        .Where(x => x.Length == 2)
        .Where(x => x[0] == "Close")
        .Select(x => decimal.Parse(x[1]))
        .ToArray();
Sign up to request clarification or add additional context in comments.

2 Comments

ReadLines instead of ReadAllLines wiil be a bit better implementation: we don't want an array of the file's content; IEnumerable<string> will be enough
@DmitryBychenko - Yes, I agree. I just kept the OP's use of ReadAllLines.
1

Try this:

File.ReadLines(@"D:\mine.txt")
  // Pick only those lines starting with "Close"
  .Where(line => line.StartsWith("Close:"))
  // Get value, which follows colon, and parse it do double
  .Select(line => double.Parse(line.Split(':')[1]))
  // Convert result to an array
  .ToArray();

1 Comment

ReadLines instead of ReadAllLines wiil be a bit better implementation: we don't want an array of the file's content; IEnumerable<string> will be enough
0

I supposed your file Like this :

AAPL,2013-1-2
Open:79.117
Close:78.433
High:79.286
Low:77.376
Volume:139948984
AAPL,2013-1-3
Open:78.268
Close:77.442
High:78.524
Low:77.286
Volume:88114464

Try this

   var lines = System.IO.File.ReadAllLines(@"C:\Users\bouyami\Documents\AB_ATELIER\1.txt").ToList();
        var linesFiltred = lines.Where(x => x.StartsWith("Close")).ToList();
        var result = linesFiltred.Select(x => x.Split(':')[1]).ToList();

Comments

-1

Try following which accepts blank lines :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace ConsoleApplication98
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.txt";
        static void Main(string[] args)
        {
            AAPL aapl = new AAPL(FILENAME);
        }
    }
    public class AAPL
    {
        static List<AAPL> aapls = new List<AAPL>();
        private DateTime date { get; set; }
        public decimal open { get; set; }
        public decimal close { get; set; }
        public decimal low { get; set; }
        public decimal high { get; set; }
        public int volume { get; set; }

        public AAPL() { }
        public AAPL(string filename)
        {
            StreamReader reader = new StreamReader(filename);
            string line = "";
            AAPL newAAPL = null;
            while ((line = reader.ReadLine()) != null)
            {
                line = line.Trim();

                if (line.Length > 0)
                {
                    if (line.StartsWith("AAPL"))
                    {
                        string dateStr = line.Substring(line.IndexOf(",") + 1);
                        date = DateTime.Parse(dateStr);
                        newAAPL = new AAPL();
                        aapls.Add(newAAPL);
                        newAAPL.date = date;
                    }
                    else
                    {
                        string[] splitArray = line.Split(new char[] { ':' });

                        switch (splitArray[0])
                        {
                            case "Open":
                                newAAPL.open = decimal.Parse(splitArray[1]);
                                break;

                            case "Close":
                                newAAPL.close = decimal.Parse(splitArray[1]);
                                break;

                            case "Low":
                                newAAPL.low = decimal.Parse(splitArray[1]);
                                break;

                            case "High":
                                newAAPL.high = decimal.Parse(splitArray[1]);
                                break;

                            case "Volume":
                                newAAPL.volume = int.Parse(splitArray[1]);
                                break;
                        }
                    }
                }
            }
        }


    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.