Newbie: txt files with 2 colums => 2 Arrays how?

Hi,

I've got a txt file with about 1000-1300 lines like this

Datum          Kurs       Antal
19980617    64,75    8078728
19980618    63,38    5058848
19980622    63,00    9548024
19980623    65,75    5752912
......

My question is how can I make the last two colums "kurs" and "Antal" into two Arrays named Akurs and Aantal


Answer this question

Newbie: txt files with 2 colums => 2 Arrays how?

  • Billl

    Hi,

    read your file line by line and split it based on the whitespaces. After that you can add them to ArrayLists or whatever you want.

    Better will be to create a structure and add this to a list but it's your choice how to implement it ;-)

    For example you can do it like in the following code:

    public struct ExchangeRateData {
    public DateTime Date;
    public double Rate;
    public int Antal;
    }

    -------------------

    ArrayList exchangeData = new ArrayList();
    string[] splitted;

    using (StreamReader sr = new StreamReader(path))
    {
    while (sr.Peek() >= 0)
    {
    splitted = sr.ReadLine().Split(" ");
    ExchangeRateData erd = new ExchangeRateData();
    erd.Date = DateTime.Parse(splitted[0]);
    erd.Rate = Double.Parse(splitted[1]);
    erd.Antal = Int32.Parse(splitted[2]);
    exchangeData.Add(erd);
    }
    }

    You have to add error checks by yourself.


  • RobertC..!

     Norbert Eder wrote:
    Hi,

    read your file line by line and split it based on the whitespaces. After that you can add them to ArrayLists or whatever you want.

    Better will be to create a structure and add this to a list but it's your choice how to implement it ;-)

    For example you can do it like in the following code:

    public struct ExchangeRateData {
    public DateTime Date;
    public double Rate;
    public int Antal;
    }

    -------------------

    ArrayList exchangeData = new ArrayList();
    string[] splitted;

    using (StreamReader sr = new StreamReader(path))
    {
    while (sr.Peek() >= 0)
    {
    splitted = sr.ReadLine().Split(" ");
    ExchangeRateData erd = new ExchangeRateData();
    erd.Date = DateTime.Parse(splitted[0]);
    erd.Rate = Double.Parse(splitted[1]);
    erd.Antal = Int32.Parse(splitted[2]);
    exchangeData.Add(erd);
    }
    }

    You have to add error checks by yourself.

    Thanks for the help guys! When I think of it I think I made a big misstake earlier. Itturns out I do only need to get the Second colum "kurs" into a Array.

    Bonus Question:

    When (if ever) I get this second coulmn into a Array. Is it possible to do some operations on this array (value2/value1(63,38/64,75), value3/value2(63/63,38), value4/value3(65,75/63)... and take these new results into a new array

     


  • ChangLuo

    You can use StreamReader class to read from the text file, line by line using ReadLine() function and then String class' Split() function to find out all the occurrences of the lying words...
    At the second and third instance save them to difference arrays (however in your case you might wanna use arraylist instead, will make  your job easier).

    Following code for reference, if any:
    string str = "19980617    64,75    8078728" // example; you read from the file
    string[] parts = part.Split(' ');
    foreach (string s in parts)
    {
                if (s.Trim() != "")
                        Console.WriteLine(s);
                }

  • Newbie: txt files with 2 colums => 2 Arrays how?