0

i have a text file like this

a1,b1,30.04.2017
c1,d1,30.05.2017

i want to add every line to a list, the first two columns will be string variables s1 and s2 and the third one will be converted to a variable showing a date

i am not experienced at all and usually i find my answers searching here. i can only find examples reading lines containing the name of the variable and the value,

i think i will need to use the code below

 List<string> fileLines = new List<string>();

using (var reader = new StreamReader(fileName))
{
    string line;
    while ((line = r.ReadLine()) != null)
    {

    }
}

i cannot do the things inside the bracket, namely , parse the line , assign first column to variable s1 assign second column to variable s2 convert the third to date and assign it to d1 then add line to list

thanks in advance

Hadi
  • 36,233
  • 13
  • 65
  • 124
kenan
  • 15
  • 6

3 Answers3

0

Keep it simple.

var text = File.ReadAllText(@"[FILEPATH]");

var fileLines = text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();

But again, this could have been easily googled.

Just in case, if you need the complete code: Here is my version

private static void TestReader()
    {
        var text = File.ReadAllText(@"D:\sample.txt");
        var fileLines = new List<SampleData>();
        foreach (var fileLine in text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
        {
            var lineData = fileLine.Split(',');

            // Make sure all the keys are present / do a separate check
            if (lineData.Length <= 2)
            {
                continue;
            }

            fileLines.Add(new SampleData
            {
                Code = Convert.ToString(lineData[0]),
                Description = Convert.ToString(lineData[1]),
                SelectedDate = lineData.Length >= 3 ? DateTime.ParseExact(lineData[2], "dd.MM.yyyy", null) : DateTime.MinValue
            });
        }
    }

    public class SampleData
    {
        public string Code { get; set; }
        public string Description { get; set; }
        public DateTime SelectedDate { get; set; }
    }
Neal Gabriel
  • 114
  • 1
  • 9
0

In code below i write the parsed data, and you can use them how you want:

List<string> fileLines = new List<string>();
using (var reader = new StreamReader(fileName))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                if (line == "") continue; // refuse bare lines...
                string a = line.Split(',')[0];
                string b = line.Split(',')[1];
                DateTime dt = DateTime.ParseExact(line.Split(',')[2], "dd.MM.yyyy", null);
                fileLines.Add(line); // if you want....
            }
        }
Farzin Kanzi
  • 3,380
  • 2
  • 21
  • 23
  • thanks for the answer . how will add the line to the list ? – kenan Apr 02 '17 at 11:44
  • the last line `fileLines.Add(line);` do this. – Farzin Kanzi Apr 02 '17 at 11:46
  • should i declare the list beforehand? like var fileLines=new[] ; – kenan Apr 02 '17 at 11:49
  • There is in your question. `List fileLines = new List();` I did not repeat this in my answer. – Farzin Kanzi Apr 02 '17 at 11:50
  • sorry i missed it – kenan Apr 02 '17 at 11:51
  • @kenan . if this or any answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Farzin Kanzi Apr 02 '17 at 12:11
  • last line filelines.Add(line) adds unsplitted line to list. how can i add it as three entries – kenan Apr 02 '17 at 15:09
  • You can add a string to `List` by `theList.Add()`. In your case you can add `a` by `filelines.Add(a)` or add `b` by `filelines.Add(b)` or add `dt` by `filelines.Add(dt.ToString())` – Farzin Kanzi Apr 02 '17 at 15:41
0

Using Linq :

var query = from line in File.ReadAllLines(filename)
where !string.IsNullOrEmpty(line)
let words = line.Split(";")
select new { Item1 = words[0], Item2 = words[1], Date = DateTime.ParseExact(words[2], "dd.MM.yyyy", null) };
Arnaud F.
  • 8,252
  • 11
  • 53
  • 102