title: How do I handle line breaks in a CSV file using C#?
body: <p>I have an Excel spreadsheet being converted into a CSV file in C#, but am having a problem dealing with line breaks.  For instance:</p>
  
  <pre><code>"John","23","555-5555"
  
  "Peter","24","555-5
  555"
  
  "Mary,"21","555-5555"
  </code></pre>
  
  <p>When I read the CSV file, if the record does not starts with a double quote (") then a line break is there by mistake and I have to remove it.  I have some CSV reader classes from the internet but I am concerned that they will fail on the line breaks.</p>
  
  <p>How should I handle these line breaks?</p>
  
  <hr>
  
  <p>Thanks everybody very much for your help.</p>
  
  <p>Here's is what I've done so far. My records have fixed format and all start with </p>
  
  <pre><code>JTW;...;....;...;
  
  JTW;...;...;....
  
  JTW;....;...;..
  
  ..;...;... (wrong record, line break inserted)
  
  JTW;...;...
  </code></pre>
  
  <p>So I checked for the <code>;</code> in the [3] position of each line. If true, I write; if false, I'll append on the last (removing the line-break)</p>
  
  <p>I'm having problems now because I'm saving the file as a txt.</p>
  
  <p>By the way, I am converting the Excel spreadsheet to csv by saving as csv in Excel. But I'm not sure if the client is doing that.</p>
  
  <p>So the file as a TXT is perfect. I've checked the records and totals. But now I have to convert it back to csv, and I would really like to do it in the program. Does anybody know how?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>namespace EditorCSV
  {
      class Program
      {
          static void Main(string[] args)
          {
              ReadFromFile("c:\\source.csv");
          }
  
          static void ReadFromFile(string filename)
          {
              StreamReader SR;
              StreamWriter SW;
              SW = File.CreateText("c:\\target.csv");
              string S;
              char C='a';
              int i=0;
              SR=File.OpenText(filename);
              S=SR.ReadLine();
              SW.Write(S);
              S = SR.ReadLine();
              while(S!=null)
              {
                  try { C = S[3]; }
                  catch (IndexOutOfRangeException exception){
                      bool t = false;
                      while (t == false)
                      {
                          t = true;
                          S = SR.ReadLine();
                          try { C = S[3]; }
                          catch (IndexOutOfRangeException ex) { S = SR.ReadLine(); t = false; }
  
                      }
                  }
                  if( C.Equals(';'))
                  {
                      SW.Write("\r\n" + S);
                      i = i + 1;
                  }
                  else
                  {
                      SW.Write(S);
  
                  }
                  S=SR.ReadLine();
              }
              SR.Close();
              SW.Close();
              Console.WriteLine("Records Processed: " + i.ToString() + " .");
              Console.WriteLine("File Created SucacessFully");
              Console.ReadKey();
  
  
          }
  
      }
  } 
  </code></pre>