c# - Unexpected "," from StreamReader to StreamWriter -


i've tried lot of things research , asking friends, can't seem write second line without "," replacing line. i'd have single line each item read in separate file.

each read file has several of these items:

2/20/2014 7:33:10 operator: jason file: c:\ax14\setups\000062363106rh.prt units: english test result: pass      channel 1         test type: velocity         result:  pass         upper limit: 0.2260         lower limit: 0.2220         max thickness: 2.0110         min thickness: 1.0110         measured velocity: 0.2225         measured thickness: 1.5215 

id have date , velocity line in 1 line this: "2/20/2014 7:33:10 am, measured velocity: 0.2225"

and problem

2/20/2014 7:33:10 am, , , , , , , , , , , , ,   measured velocity: 0.2225 , 2/20/2014 7:52:28 am, , , , , , , , , , , , ,   measured velocity: 0.2224 , 2/20/2014 7:58:46 am, 

   using system; using system.collections.generic; using system.text; using system.io; using system.collections;  namespace conapp {     class program     {          static void main(string[] args)         {             string line;             try             {                 using (streamwriter sw = new streamwriter("c:\\writetest\\writetest.txt"))                 {                     string mydirpath = "c:\\chat\\";                      string[] txtfilelist = directory.getfiles(mydirpath, "*.txt");                      foreach (string txtname in txtfilelist)                     {                         system.io.streamreader sr = new system.io.streamreader(txtname);                              while ((line = sr.readline()) != null)                             {                                 if (!string.isnullorempty(line))                                 {                                     string spart = ".prt";                                     string sam = " am";                                     string spm = " pm";                                     string sresult = "test result: ";                                     string svelocity = "measured velocity: ";                                     string part = "";                                     string date = "";                                     string result = "";                                     string velocity = "";                                     // sw.writeline(line);                                      if (line.contains(sam))                                     {                                         date = line;                                     }                                      if (line.contains(spm))                                     {                                         date = line;                                     }                                      if (line.contains(spart))                                     {                                         part = line;                                     }                                      if (line.contains(sresult))                                     {                                         result = line;                                     }                                      if (line.contains(svelocity))                                     {                                         velocity = line;                                     }                                     int = 2;                                     string[] x = new string[i];                                     x[0] = date;                                     x[1] = velocity;                                      sw.writeline(x[0] + "," + x[1]);                                 }                               }                      }                 }             }             catch             {              }         }     } } 

here suggestion full main() trying use code possible. declaring vars outside while statement don't need make null.

edit- forgot said:

each read file has several of these items

so added few lines handle that.

static void main(string[] args) {     string line;     try     {         using (streamwriter sw = new streamwriter("c:\\writetest\\writetest.txt"))         {             string mydirpath = "c:\\chat\\";              string[] txtfilelist = directory.getfiles(mydirpath, "*.txt");              foreach (string txtname in txtfilelist)             {                 string spart = ".prt";                 string sam = " am";                 string spm = " pm";                 string sresult = "test result: ";                 string svelocity = "measured velocity: ";                 string part = string.empty;                 string date = string.empty;                 string result = string.empty;                 string velocity = string.empty;                  using (streamreader sr = new streamreader(txtname))                 {                     while ((line = sr.readline()) != null)                     {                         if (!string.isnullorempty(line) && line.trim().length != 0)                         {                             if (line.contains(sam) || line.contains(spm))                             {                                 // every new date means new record. if have data record, first write it.                                 if (date != string.empty && velocity != string.empty)                                 {                                     int = 2;                                     string[] x = new string[i];                                     x[0] = date;                                     x[1] = velocity;                                     sw.writeline(x[0] + "," + x[1]);                                 }                                 // reset data prepare new record                                 part = string.empty;                                 result = string.empty;                                 velocity = string.empty;                                 date = line;                             }                              if (line.contains(spart))                             {                                 part = line;                             }                              if (line.contains(sresult))                             {                                 result = line;                             }                              if (line.contains(svelocity))                             {                                 velocity = line;                             }                         }                     }                 }                  // after last record still have data write                 if (date != string.empty && velocity != string.empty)                 {                     int = 2;                     string[] x = new string[i];                     x[0] = date;                     x[1] = velocity;                     sw.writeline(x[0] + "," + x[1]);                 }             }         }     }     catch     {      } } 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -