Losbaltica
Losbaltica

Reputation: 649

Convert .XYZ to .csv using c#

Hi i am using this method to replace " " to "," but is failing when i try to use it on data that have 32 millions lines. Is anyone knows how to modify it to make it running?

            List<String> lines = new List<String>();

            //loop through each line of file and replace " " sight to ","
            using (StreamReader sr = new StreamReader(inputfile))
            {
                int id = 1;

                int i = File.ReadAllLines(inputfile).Count();
                while (sr.Peek() >= 0)
                {
                    //Out of memory issuee
                    string fileLine = sr.ReadLine();

                    //do something with line
                    string ttt = fileLine.Replace(" ", ", ");

                    //Debug.WriteLine(ttt);
                    lines.Add(ttt);
                    //lines.Add(id++, 'ID');
                }

                using (StreamWriter writer = new StreamWriter(outputfile, false))
                {
                    foreach (String line in lines)
                    {
                        writer.WriteLine(line+","+id);
                        id++;                     
                    }                       
                }                
            }

            //change extension to .csv
            FileInfo f = new FileInfo(outputfile);
            f.MoveTo(Path.ChangeExtension(outputfile, ".csv"));   

I general i am trying to convert big .XYZ file to .csv format and add incremental field at the end. I am using c# for first time in my life to be honest :) Can you help me?

Upvotes: 0

Views: 1333

Answers (1)

PaulF
PaulF

Reputation: 6773

See my comment above - you could modify your reading / writing as follows :

  using (StreamReader sr = new StreamReader(inputfile))
  {
    using (StreamWriter writer = new StreamWriter(outputfile, false))
    {
      int id = 1;

      while (sr.Peek() >= 0)
      {
        string fileLine = sr.ReadLine();

        //do something with line
        string ttt = fileLine.Replace(" ", ", ");
        writer.WriteLine(ttt + "," + id);
        id++;
      }
    }
  }

Upvotes: 1

Related Questions