Hesham Hassanein

Hesham Hassanein

  • NA
  • 140
  • 16.9k

Splitting a file into many files depending on a string

Sep 16 2016 10:40 AM

I am reading a DAT file that has the following format.

XCVR TX K-FACTOR CORR|DC|INFO||0.836709|PASS|1
VERSION TEST|0|GREATER THAN||1141001|PASS|1
END-OF-LINE
VERSION TEST|0|GREATER THAN||1141001|PASS|1
END-OF-LINE
..
END-OF-LINE
Records=3 
 
It can has as many end of lines as it can. So the idea is to split when it finds End-OF-LINE
and put the result in a csv file. I have to do it in 1 Method.
 
The split works, but it shows only 2 generated CSV files. The first one is the one that contains the data before the first end-of-line comes and the second file is the one before the last end-of-line. I need to open a new streamwriter everytime it finds the string: END-OF-LINE. What is missing in this code: 

I  do not know how to generate each time a split occurs. Thanks in Advance

  1. using (System.IO.StreamWriter file = new System.IO.StreamWriter(filepath))  
  2.             {  
  3.   
  4.                 while (!((lines = reader.ReadLine()).Contains("END-OF-LINE")))  
  5.                 {  
  6.   
  7.                     foreach (string line in lines.Split('|'))  
  8.                     {  
  9.                         if (line.Contains("END-OF-LINE"))  
  10.                         {  
  11.                         StreamWriter xx = new StreamWriter(path);  
  12.        
  13.                             xx.WriteLine(line);  
  14.                         }  
  15.                     }  
  16.   
  17.   
  18. // Do operations on the file and then comes the splitting  
  19.   
  20. //////////////////////////////// Splitting the file ///////////////////////////////  
  21.                     int h = 0;  
  22.                     System.IO.StreamWriter outfile = null;  
  23.                     try  
  24.                     {  
  25.                         using (var infile = new System.IO.StreamReader(fileName))  
  26.                         {  
  27.                             while (!infile.EndOfStream)  
  28.                             {  
  29.                                 lines = infile.ReadLine();  
  30.                                   
  31.                                if (lines.Contains("END-OF-LINE"))  
  32.                                 {  
  33.                                     if (outfile != null)  
  34.                                     {  
  35.                                         outfile.Dispose();  
  36.                                         outfile = null;  
  37.                                     }  
  38.                                     continue;  
  39.                                 }  
  40.                                if (lines.Contains("Records"))  
  41.                                {  
  42.                                    break;  
  43.                                }  
  44.   
  45.                                 while (outfile == null)  
  46.                                 {  
  47.                                 outfile = new System.IO.StreamWriter(string.Format(path, h++),  
  48.                                 false,  
  49.                                 infile.CurrentEncoding);   
  50.                                       
  51.                                 }  
  52.                                 outfile.WriteLine(lines);  
  53.                             }  
  54.                         }  
  55.                     }  
  56.                     finally  
  57.                     {  
  58.                         if (outfile != null)  
  59.                             outfile.Dispose();  
  60.                     }  
  61.   
  62.                       
  63.                  } // To close using   

Answers (1)