-1

I am reading a CSV file and want to read it line by line. The code below does not have any error but when execute the code it reads from middle of the CSV, it just prints last four lines of CSV but i need the whole CSV data as output. please assist what i an missing in my code

I want to achieve this using streamreader only and not parser.

using (StreamReader rd = new StreamReader(@"C:\Test.csv"))
            {
                while (!rd.EndOfStream)
                {
                    String[] value = null;
                    string splits = rd.ReadLine();
                    value = splits.Split(',');

                    foreach (var test in value)
                    {
                        Console.WriteLine(test);
                    }
                }
            }

Test.csv

TEST Value ,13:00,,,14:00,,,15:00,,, "Location","Time1","Transaction1","Transaction2","Tim2", "Pune","1.07","-","-","0.99", "Mumbai","0.55","-","-","0.59", "Delhi","1.00","-","-","1.08", "Chennai","0.52","-","-","0.50",

NoviceToProgramming
  • 103
  • 1
  • 3
  • 12
  • 3
    It must be printing all values, it's just that Console buffer gets overwritten. Write it in some file and see if it gives you all what you want – Typist Jun 18 '14 at 17:55
  • Using the console to debug your output may be flawed - you might not have enough history buffered to display everything it's outputting. – 48klocs Jun 18 '14 at 17:57
  • no its just print last four lines not all data. – NoviceToProgramming Jun 18 '14 at 17:57
  • Remember that CSV files can legitimately have line endings (and commas) as field content: http://tools.ietf.org/html/rfc4180 – lesscode Jun 19 '14 at 03:36
  • possible duplicate of [Read CSV using streamreader](http://stackoverflow.com/questions/24284032/read-csv-using-streamreader) – ScottMcGready Jun 19 '14 at 15:35

2 Answers2

1

There is already a stack overflow article about this.

Also, the article provides a much better way to do this same:

using (TextFieldParser parser = new TextFieldParser(@"c:\test.csv"))
{
    parser.TextFieldType = FieldType.Delimited;
    parser.SetDelimiters(",");
    while (!parser.EndOfData) 
    {
        //Processing row
        string[] fields = parser.ReadFields();
        foreach (string field in fields) 
        {
            //TODO: Process field
        }
    }
}
Community
  • 1
  • 1
Consult Yarla
  • 1,150
  • 10
  • 22
  • Asker wrote: "I want to achieve this using streamreader only and not parser." – msmolcic Jun 18 '14 at 18:02
  • 1
    This isn't actually answering the question, merely providing tangential information, and as such it should really be a comment. – Servy Jun 18 '14 at 18:06
0

I believe there is something wrong with your CSV file, it may contain some unexpected characters.Several things you can try:

You can let StreamReader class to detect correct encoding of your CSV

new StreamReader(@"C:\Test.csv", System.Text.Encoding.Default, true)

You can force your StreamReader class to read your CSV from the beginning.

rd.DiscardBufferedData(); 
rd.BaseStream.Seek(0, SeekOrigin.Begin); 
rd.BaseStream.Position = 0;

You can try to fix your CSV file, such as clean null character and Convert Unix newline to Windows newline.

OKEEngine
  • 888
  • 11
  • 28