CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2011
    Posts
    10

    to read csv file into text file

    I have written the code to read the csv file(which include 4 rows)
    and the application is running sucessfully, while writting to the file it writes 2nd and 4th row to the file
    I am not getting why its happing.
    Can anyone help me for the same.
    following is the code
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.Xml;
    using System.Configuration;
    using System.IO;
    
    namespace Test1
    {
    
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Uploadbutton_Click(object sender, EventArgs e)
            {
                openFileDialog1.ShowDialog();
                filenametextBox.Text = openFileDialog1.FileName;
    
            }
    
            private void save_to_DBbutton_Click(object sender, EventArgs e)
            {
                
                if (openFileDialog1.FileName == "")
                {
                    MessageBox.Show("File Not Selected");
                }
                StreamReader sr = new StreamReader(openFileDialog1.FileName,Encoding.GetEncoding(1250));
                StreamWriter SW;
                SW = File.CreateText("D:\\CSV\\csv.txt");
    
                string strline = "";
                string str1 = "";
                string[] _values = null;
                
                while ((strline = sr.ReadLine()) != null)
                {
    
                    strline = sr.ReadLine();
                    _values = strline.Split(';');
                    foreach (string str in _values)
                    {
                        str1 = str + ",";
                    }
                    str1 = str1.Trim(',');
                    SW.WriteLine(str1);
                
                }            sr.Close();
                SW.Close();
                MessageBox.Show("Sucessfully Retrieved and file has been created");
                this.Close(); 
            }
        }
    }

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: to read csv file into text file

    Quote Originally Posted by Arjun-Sarankulu View Post
    I am not getting why its happing.
    Me neither. Perhaps you could tell us what is happening?

    on a side note, if this is a practice project, writing your own csv reader is fine. However, if this is 'real' code, I have had good experiences using Lumen CSV. Parsing CSV file is not as simple as splitting on commas. It is legal to include the separator within commas (among other edge cases), and your code doesn't handle that case. Your code is too simple to be a proper CSV parser.

  3. #3
    Join Date
    Jan 2011
    Posts
    10

    Re: to read csv file into text file

    where i can find the Lumen CSV and it should read all the data from csv file
    I am not getting what is wrong with my code.
    I am developeing a utility for my company which will read the csv file
    Can any one help me for the same.

  4. #4
    Join Date
    Oct 2004
    Posts
    206

    Re: to read csv file into text file

    Try removing the call to
    Code:
    strline = sr.ReadLine();
    that you have inside your loop

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: to read csv file into text file

    Quote Originally Posted by Arjun-Sarankulu View Post
    I am developeing a utility for my company which will read the csv file
    Then your implementation is not good enough. You can find Lumen CSV by searching for it.

  6. #6
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: to read csv file into text file

    I would recommend a slightly different solution than dlarkin77 (though it fixes the same problem). The way I always write these loops is:

    Code:
    while ( !sr.EndOfStream )
    {
         strline = sr.ReadLine();
         ... //Downstream processing
    }
    I guess my preference is to always make the read call from within the loop. This seems more logical to me, but dlarkin77's method may work as well.

    You might consider listening to BigEd781's advice too: CSV parsing can be a real pain if you're supporting the whole standard. On the other hand, if you're just reading in a matrix of numbers separated by commas, your fast solution may be 'good enough'.

    Hope that helps.
    Last edited by BioPhysEngr; February 5th, 2011 at 01:40 AM. Reason: forgot a code tag
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured