c# - Writing to a file and formatting rows .Suggestion needed -


wondering if better suggestions have come with.

i have requirement write text file in particular format each field must start @ position stated , must padded blank spaces till next one.

example:

        field            position in row         name             1           surname          20         address          50         country          90         dob              120         maritalstatus    160 

below prototype attempt, there neater better way of doing this? need test position in row correct in unit test?

any suggestions?

              class program                 {                     static void main(string[] args)                     {                         customer customer=new customer();                         customer.name = "jo";                         customer.surname = "bloggs";                         customer.address = " 1 newyork road";                         customer.country = "uk";                         customer.dob = "29/04/1990";                         customer.maritalstatus = "married";                          stringbuilder sb=new stringbuilder();                         createheader(customer,sb);                         sb.appendline("");                         createrow(customer, sb);                         sb.appendline("");                          ioextensions.writetofile(sb.tostring(), "testfile.txt");                      }                      private static void createheader(customer customer,stringbuilder sb)                     {                         /*                            *                            field            position in row                           name             1                               surname          20                           address          50                           country          90                           dob              120                           maritalstatus    160                             */                         //first field                         sb.append(formatvalue("name", 19));                         sb.append(formatvalue("surname", 29));                         sb.append(formatvalue("address", 39));                         sb.append(formatvalue("country", 29));                         sb.append(formatvalue("dob", 39));                          //last field not matter                         sb.append(formatvalue("maritalstatus", 9));                       }                     private static void createrow(customer customer, stringbuilder sb)                     {                         /*                            *                            field            position in row                           name           1                             surname          20                           address          50                           country          90                           dob              120                           maritalstatus    160                             */                         //first field                         sb.append(formatvalue(customer.name, 19));                         sb.append(formatvalue(customer.surname, 29));                         sb.append(formatvalue(customer.address, 39));                         sb.append(formatvalue(customer.country, 29));                         sb.append(formatvalue(customer.dob, 39));                          //last field not matter                         sb.append(formatvalue(customer.maritalstatus, 19));                       }                      private static string formatvalue(string value, int maxlength)                     {                         //todo add other stuff here                         return value.padright(maxlength, ' ');                     }                 }                  public static class ioextensions                 {                     public static void writetofile(string text, string path)                     {                         using (var fs = file.createtext(path))                         {                             fs.write(text);                         }                     }                 }                 public class customer                 {                     public string name { get; set; }                     public string surname { get; set; }                     public string address { get; set; }                     public string country { get; set; }                     public string dob { get; set; }                     public string maritalstatus { get; set; }                 }             } 

i time, here suggestion.... take class , override "tostring" function or create custom function called else. use "padright" string function create fields of fixed length right padding.

example (you'll need add rest of fields tostring):

            public class customer             {                 public string name { get; set; }                 public string surname { get; set; }                 public string address { get; set; }                 public string country { get; set; }                 public string dob { get; set; }                 public string maritalstatus { get; set; }                  public override string tostring()                 {                     return string.format("{0}{1}{2}",                         name.padright(20),                         surname.padright(30),                         address.padright(40)                         );                 }             } 

note: using length control starting position of each field. if first filed 20 characters, next field start @ 21 , go length define.

now, loop on data , fill object , call customer.tostring() write out formatted string.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -