Hesham Hassanein

Hesham Hassanein

  • NA
  • 140
  • 16.1k

Reading data from 2 files and Creating .csv file with it

Sep 7 2016 5:09 AM

Hello,

I am developing a small code which reads data from 2 different files. One file is .CSV and the other is.DAT.

I am storing the data from these 2 files into multidimensional array depending on their values. then creating a text file that should contain this data.

I wrote the following code, but i do not see any result in the console, Thanks in Advance

 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.IO;  
  5. using System.Windows.Forms;  
  6. using dowStepType;  
  7. using MySql.Data;  
  8. using MySql.Data.MySqlClient;  
  9. using System.Data;  
  10. using System.Diagnostics;  
  11.   
  12.   
  13. // using java.io.IOException;  
  14. // using java.io.File;  
  15. // using java.io.FileWriter;  
  16. //http://msdn.microsoft.com/en-us/library/8bh11f1k.aspx  
  17.   
  18. namespace ConsoleApplication1  
  19. {  
  20.   
  21.     public static class Program  
  22.     {  
  23.         /// <summary>  
  24.         /// Der Haupteinstiegspunkt für die Anwendung.  
  25.         /// </summary>  
  26.         [STAThread]  
  27.   
  28.   
  29.   
  30.         public static void Main(string[] argv)  
  31.         {  
  32.   
  33.   
  34.   
  35.   
  36.             string[,] Result;  
  37.   
  38.             //StreamReader reader1 = File.OpenText(@"D:\Profiles\cdg033\Desktop\MQS Project\PARAM-20160906-000505.DAT");  
  39.            // StreamReader reader2 = File.OpenText(@"D:\Profiles\cdg033\Desktop\MQS Project\BRST26_CXB1413C7M.rslt.07-15-2014.11.08.24.CSV");  
  40.             var lineCount = File.ReadLines(@"D:\Profiles\cdg033\Desktop\MQS Project\BRST26_CXB1413C7M.rslt.07-15-2014.11.08.24.CSV").Count();  
  41.   
  42.             Program.ReadDATA(out Result);  
  43.             Program.GenerateCSV(out Result);  
  44.   
  45.   
  46.         }  
  47.   
  48.   
  49.   
  50.   
  51.   
  52.         // Reading Values from DAT and CSV Files  
  53.         static string ReadDATA(out string[,] Result)  
  54.         {  
  55.   
  56.             //Console.WriteLine("Curropted Data File");  
  57.             //string dat_file_path = @"D:\Profiles\cdg033\Desktop\MQS Project\PARAM-20160906-000505.DAT";  
  58.             //string csv_file_path = @"D:\Profiles\cdg033\Desktop\MQS Project\BRST26_CXB1413C7M.rslt.07-15-2014.11.08.24.CSV";  
  59.               
  60.   
  61.           StreamReader reader1 = new StreamReader (@"D:\Profiles\cdg033\Desktop\MQS Project\PARAM-20160906-000505.DAT");  
  62.           StreamReader reader2 = new StreamReader (@"D:\Profiles\cdg033\Desktop\MQS Project\BRST26_CXB1413C7M.rslt.07-15-2014.11.08.24.CSV");  
  63.   
  64.             string line1;  
  65.             string line2;  
  66.             int Profile_Index = 1;  
  67.             string[,] Result = new string[lineCount, 16];  
  68.   
  69.   
  70.            
  71.                
  72.             while ((line1 = reader1.ReadLine()) != null && (line2 = reader2.ReadLine()) != null)  
  73.             {  
  74.                 string[] fields1 = line1.Split('|');  
  75.                 string[] fields2 = line2.Split(',');  
  76.                 for (int i = 0; i < lineCount - 1; i++)  
  77.                 {  
  78.                     for (int j = 1; j < 16; j++)  
  79.                     {  
  80.   
  81.                         Result[0, 0] == "PATS_TEST";  
  82.                         if (fields1[0].Contains("G"))  
  83.                         {  
  84.                             Result[i, 1] == fields1[1];  
  85.                             Result[i, 2] == fields1[5];  
  86.                             Result[i, 3] == fields1[4];  
  87.                             Result[i, 4] == "1";  
  88.                             Result[i, 5] == fields1[12];  
  89.                             Result[i, 6] == fields2[6];  
  90.                             Result[i, 7] == fields1[2];  
  91.                             Result[i, 8] == "1";  
  92.                             Result[i, 9] == "0";  
  93.                             Result[i, 10] == "1";  
  94.                             Result[i, 11] == "TEST TIME";  
  95.                             Result[i, 12] == fields1[7];  
  96.                             Result[i, 13] == "1";  
  97.                             Result[i, 14] == "0";  
  98.                             Result[i, 15] == Profile_Index;  
  99.   
  100.                         }  
  101.   
  102.                         else if (fields1[0].Contains("END-OF-LINE"))  
  103.                         {  
  104.   
  105.                             Result[i, end] = ""// Goto Next Line  
  106.                               
  107.                         }  
  108.   
  109.                         else if (fields1[0].Contains("Records"))  
  110.                         {  
  111.   
  112.                             break;  
  113.                         }  
  114.                         else {  
  115.   
  116.                             Result[i, 1] == fields1[0];  
  117.                             Result[i, 2] == fields2[2];  
  118.                             Result[i, 3] == fields1[0];  
  119.                             Result[i, 4] == fields1[6];  
  120.                             Result[i, 5] == fields2[5]; //Test Value  
  121.                             Result[i, 6] == fields2[6]; //Frequency  
  122.                             Result[i, 7] == fields1[5];  
  123.                             Result[i, 8] == "1";        //Voltage  
  124.                             Result[i, 9] == "0";        //Lower Limit   
  125.                             Result[i, 10] == "1";       //Upper Limit  
  126.                             Result[i, 11] == "TEST TIME";  
  127.                             Result[i, 12] == "Attempts";  
  128.                             Result[i, 13] == "1";        // Measurement Unit  
  129.                             Result[i, 14] == "0";       //Retest Flag  
  130.                             Result[i, 15] == Profile_Index;  
  131.                           
  132.                          
  133.                         }  
  134.                     }  
  135.                 }  
  136.                 Console.WriteLine("The Contents of the buffer are" + Result[i, j]);  
  137.                 Profile_Index++;  
  138.   
  139.             }  
  140.   
  141.             reader1.Close();  
  142.             reader2.Close();  
  143.   
  144.   
  145.   
  146.   
  147.         }  
  148.   
  149.          //Generation of CSV File  
  150.         static void generatecsv(string[,] Result)  
  151.             {  
  152.                 string ProgramPfad = (@"D:\Profiles\cdg033\Desktop\MQS Project\Test_Result.CSV");  
  153.   
  154.                 ReadDATA (out Result);  
  155.                 string[,] buffer = new string[Result.GetLength(0),Result.GetLength(1)];  
  156.                 using (System.IO.StreamWriter file = new System.IO.StreamWriter(ProgramPfad))  
  157.                 {  
  158.   
  159.   
  160.                     for (int i = 0; i < buffer.Length - 1; i++)  
  161.                     {  
  162.                         for (int j=0; j< buffer.Length -1; j++ )  
  163.   
  164.                         {  
  165.                                 buffer[i,j] = Result[i,j];  
  166.                                 file.WriteLine(buffer[i,j]);  
  167.   
  168.   
  169.                      Console.WriteLine("The Contents of the buffer are" + buffer[i]);    
  170.                     }  
  171.                     }  
  172.                 }  
  173.   
  174.             }  
  175.   
  176.   
  177.   
  178.         }  
  179.     }  

Answers (4)