Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I am trying to read pipe delimited file but cant see the output can see only blank screen

Also if I want to output to a file how do I do it.

below is my code what I am doing wrong.

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] strfilepath = Directory.GetFiles(@"C:\test.txt");
            List<string> list = new List<string>();
            try
            {
                foreach (string FiletoRead in strfilepath)
                {
                    
                    System.IO.StreamReader file = new System.IO.StreamReader(FiletoRead);
                    string read;
                    while ((read = file.ReadLine()) != null)
                    {
                        string[] lineData = read.Split('|');
                        for (int i = 0; i < lineData.Length; i++)
                        {
                            list.Add(lineData[i]);
                        }
                        file.Close();
                        {

                            Console.WriteLine("{0}", lineData[0]);
                            Console.Read();
                        }
                    }

                }
            }
            catch(Exception)
            {
                Console.WriteLine("No File");
            }


        }
    }
}


please help.
Posted
Updated 13-Jan-14 16:43pm
v3

You are outputting lineData[0] which, presumably, is empty (or at least unprintable).

Run un debug, puit a breakpoint inside the loop, and check the values of lineData[i] to see what it is actually reading
 
Share this answer
 
Comments
Ron Beyer 13-Jan-14 22:54pm    
And he closes the file after a single line is read, and he should be printing out the list data, not line data.
Mary Abraham 13-Jan-14 22:56pm    
Hi,
How to do that and also it says invalid path
Mary Abraham 13-Jan-14 22:55pm    
HiI debug and saw it says invalid path the path is correct.
_Maxxx_ 13-Jan-14 23:11pm    
If it says the path isn't correct, then it isn't. It might look like it is, but it isn't.
You are using Directory.GetFiles and passing the name of a file, not the path of a directory. GetFiles returns a list of files in a directory.
Mary Abraham 14-Jan-14 19:38pm    
Hi Solved it the path was not correct.

Please see this guide:

http://msdn.microsoft.com/en-us/library/94223t4d.aspx[^]

Start there, print each line to the console. Then start adding the splitting of each line by the | characters and print those out, then add the list. Try doing the program in small chunks, you'll find it easier to do.
 
Share this answer
 
Comments
Mary Abraham 17-Jan-14 13:48pm    
Thanks all for helping me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900