working with a delimited text file
Asked Answered
T

4

6

started working on the text file reading- delimited by pipe underscore and pipe - |_| tried the following code.doesn't produce the desired result. the split condition should be modified, but how. please advise. Thanks everyone - this works now.

class Program
{
    static void Main(string[] args)
    {
        string filePath = @"J:\dot\emp.dat";


        var query = 
            from line in File.ReadLines(filePath)
            let empRecord = line.Split(new string[] {"|_|"},StringSplitOptions.None)

            select new datFile()
            {
                name = empRecord[0],
                employeeid = empRecord[1],
                income = empRecord[2],
                expenses = empRecord[3]

            };

        foreach (var item in query)
        {
            Console.WriteLine(item.name, item.employeeid, 
                 item.income, item.expenses);
        }
        Console.ReadLine();
    }

    public class datFile
    {
        public string name { get; set; }
        public string employeeid { get; set; }
        public string income { get; set; }
        public string expenses { get; set; }

      }
   }

File contents:

name|_|employeeid|_|income|_|expenses
emp1|_|201501|_|100000|_|50000
emp2|_|20000|_|90000|_|30000
emp3|_|34234|_|100000|_|23000

Output:

 name
 emp1
 emp3
 emp3   
 ----- 
Tew answered 11/8, 2011 at 17:43 Comment(3)
Please don't add "C#" to your title just to say which language you are using. That's what we use tags for here on Stack Overflow.Greene
Did you edit your question with the correct answer? You should leave the original question as it was, otherwise the answers don't make sense..Or at least comment where you edited....Alarcon
right, i see your point. thanks for that. removed the changes.Tew
D
15

The problem is actually just here:

Console.WriteLine(item.name, item.employeeid, item.income, item.expenses);

That's using item.name as a format string, which happens not to include {0}, {1} or {2}, so the remaining arguments are effectively useless. Try this, which specifies a format string and then the values to fill in:

Console.WriteLine("{0} {1} {2} {3}", 
                  item.name, item.employeeid, item.income, item.expenses);
Dorotea answered 11/8, 2011 at 17:46 Comment(3)
<<That's using item.name as a format string, which happens not to use>> I don't get it o_OComma
@VoodooChild: The first parameter of that overload of Console.WriteLine is a format string.Dorotea
@Jon Skeet: ah I see the signature is void WriteLine(string format, params object[] arg); and is expecting a composite format string - it makes sense now, thanks.Comma
S
0

Have you tried splitting with a char[] instead of string[]?

let empRecord = line.Split(new char[] {'|', '_', '|'});
Seward answered 11/8, 2011 at 17:49 Comment(1)
This will treat each character as a separate delimiter when the entirety of |_| is needed.Mccoy
U
0

I don't work much with Console applications but try changing your code as follows:

foreach (var item in query)
{
    Console.WriteLine("{0}, {1}, {2}, {3}",
        item.name, item.employeeid, item.income, item.expenses);
}
Undercut answered 11/8, 2011 at 17:49 Comment(0)
F
0

I'm embarrassed to say I had to test this quite a bit to figure out what was wrong, but that's because what was wrong was so obvious.

Everything in this example is correct except this line:

Console.WriteLine(item.name, item.employeeid, item.income, item.expenses);

Try:

Console.WriteLine("name={0}, employeeid={1}, income={2}, expenses={3}",
   item.name, item.employeeid, item.income, item.expenses);
Freemasonry answered 11/8, 2011 at 17:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.