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
- using (System.IO.StreamWriter file = new System.IO.StreamWriter(filepath))
- {
-
- while (!((lines = reader.ReadLine()).Contains("END-OF-LINE")))
- {
-
- foreach (string line in lines.Split('|'))
- {
- if (line.Contains("END-OF-LINE"))
- {
- StreamWriter xx = new StreamWriter(path);
-
- xx.WriteLine(line);
- }
- }
-
-
-
-
-
- int h = 0;
- System.IO.StreamWriter outfile = null;
- try
- {
- using (var infile = new System.IO.StreamReader(fileName))
- {
- while (!infile.EndOfStream)
- {
- lines = infile.ReadLine();
-
- if (lines.Contains("END-OF-LINE"))
- {
- if (outfile != null)
- {
- outfile.Dispose();
- outfile = null;
- }
- continue;
- }
- if (lines.Contains("Records"))
- {
- break;
- }
-
- while (outfile == null)
- {
- outfile = new System.IO.StreamWriter(string.Format(path, h++),
- false,
- infile.CurrentEncoding);
-
- }
- outfile.WriteLine(lines);
- }
- }
- }
- finally
- {
- if (outfile != null)
- outfile.Dispose();
- }
-
-
- }