Level 8 - Files and exceptions 0 of 9 activities solved
  1. Saving data Not yet solved
  2. School club Not yet solved
  3. Shopping list Not yet solved
  4. Attributes Not yet solved
  5. Cookies Not yet solved
  6. Morse code Not yet solved
  7. ini file Not yet solved
  8. Amino acids Not yet solved
  9. Gamertag Not yet solved
Next series Level 9 - Two-dimension arrays and lists
Level 8 - Files and exceptions

Saving data

Sign in to hand in your solution and get feedback.

// Saving data program using System; using System.IO; class Submission { // ------------------------- // Subprograms // ------------------------- static string load(string filename) { StreamReader file = new StreamReader(filename); string user = file.ReadLine(); user = user.Trim(); file.Close(); return user; } static void save(string user, string filename) { StreamWriter file = new StreamWriter(filename); file.WriteLine(user); file.Close(); } // ------------------------- // Main program // ------------------------- public static void Main(string[] args) { string user = load("datafile.txt"); Console.WriteLine($"This program was written by {user}."); } }