Beispiel #1
0
 /*
  * Read name pairs in from a file in the following
  * format: "lastName, firstName"
  */
 public static NamePair[] readFile(string filePath, char[] split)
 {
     List<NamePair> names = new List<NamePair>();
     string[] lines;
     try
     {
         lines = File.ReadAllLines(filePath);
         foreach(string s in lines)
         {
             NamePair name = new NamePair();
             string[] temp = s.Split(split);
             // If a name does not have two parts,
             // leave the field as null and handle
             // formatting later.
             if(temp.Length >= 1)
             {
                 name.lastName = temp[0].Trim();
                 if(temp.Length >= 2)
                 {
                     name.firstName = temp[1].Trim();
                 }
             }
             names.Add(name);
         }
     }
     catch(ArgumentException)
     {
         Console.WriteLine("Invalid file path");
         Environment.Exit(1);
     }
     catch(DirectoryNotFoundException)
     {
         Console.WriteLine("Invalid file path");
         Environment.Exit(1);
     }
     catch(IOException)
     {
         Console.WriteLine("Invalid file path");
         Environment.Exit(1);
     }
     catch(UnauthorizedAccessException)
     {
         Console.WriteLine("Could not open the file " + filePath + " due to authorisation errors");
         Environment.Exit(1);
     }
     return names.ToArray();
 }
Beispiel #2
0
 /*
  * Write the data out to a given file in the following format
  * "lastName, firstname"
  */
 public static void writeFile(string filePath, NamePair[] names)
 {
     StreamWriter output;
     try
     {
         output = new StreamWriter(filePath);
         foreach(NamePair name in names)
         {
             // If a given name doesn't have a field
             // specified, skip it
             if(name.lastName != null)
             {
                 output.Write(name.lastName);
             }
             output.Write(", ");
             if(name.firstName != null)
             {
                 output.Write(name.firstName);
             }
             output.WriteLine();
         }
         output.Close();
     }
     catch(ArgumentException)
     {
         Console.WriteLine("Invalid file path");
         Environment.Exit(1);
     }
     catch(DirectoryNotFoundException)
     {
         Console.WriteLine("Invalid file path");
         Environment.Exit(1);
     }
     catch(IOException)
     {
         Console.WriteLine("Invalid file path");
         Environment.Exit(1);
     }
     catch(UnauthorizedAccessException)
     {
         Console.WriteLine("Could not open the file " + filePath + " due to authorisation errors");
         Environment.Exit(1);
     }
 }
Beispiel #3
0
 /*
  * Order a set of names by last name, then firstname lexicographically.
  */
 public static NamePair[] orderNames(NamePair[] names)
 {
     // let the compiler handle the type of linq expressions. They can
     // be fiendishly difficult to get right.
     var sortedNames =
         from name in names
         orderby name.lastName ascending, name.firstName ascending
         select name;
     return sortedNames.ToArray();
 }