Exercise: Weather Log Writer

Write a string of temperature readings to a text file safely using a modern using declaration.

Problem statement

A weather station records daily temperature readings in Celsius. The monitoring system needs a reusable utility method to save these raw readings directly to a local log file on the disk so they can be processed by legacy systems that expect raw byte streams.

Task requirements

  • In WeatherUtility.cs, implement the SaveTemperatures method.

  • Convert the provided dailyTemperatures string into a UTF-8 encoded byte array.

  • Save the bytes to the file path provided by the fileName parameter.

  • Print “Weather log saved successfully.” to the console from within the utility method.

  • In Program.cs, read the newly saved file and print its contents to the console to verify the write operation.

Constraints

  • Use the System.Text.Encoding.UTF8.GetBytes method to convert the string.

  • Instantiate a FileStream with FileMode.Create.

  • Use a modern using declaration (using var) to ensure the file stream is safely disposed of without using curly braces.

  • Use the synchronous Write method of the FileStream to perform the disk operation.

  • Use File.ReadAllText to read the file back into a string in Program.cs.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • Remember that the FileStream constructor takes the file name and a FileMode enum value.

  • To create the byte array, you need to access Encoding.UTF8.GetBytes(yourString).

  • The Write method requires three arguments: the byte array, the offset (which is usually 0), and the total number of bytes to write (the array’s Length).

  • You can quickly verify the file in Program.cs by passing the logFile variable into File.ReadAllText().

Exercise: Weather Log Writer

Write a string of temperature readings to a text file safely using a modern using declaration.

Problem statement

A weather station records daily temperature readings in Celsius. The monitoring system needs a reusable utility method to save these raw readings directly to a local log file on the disk so they can be processed by legacy systems that expect raw byte streams.

Task requirements

  • In WeatherUtility.cs, implement the SaveTemperatures method.

  • Convert the provided dailyTemperatures string into a UTF-8 encoded byte array.

  • Save the bytes to the file path provided by the fileName parameter.

  • Print “Weather log saved successfully.” to the console from within the utility method.

  • In Program.cs, read the newly saved file and print its contents to the console to verify the write operation.

Constraints

  • Use the System.Text.Encoding.UTF8.GetBytes method to convert the string.

  • Instantiate a FileStream with FileMode.Create.

  • Use a modern using declaration (using var) to ensure the file stream is safely disposed of without using curly braces.

  • Use the synchronous Write method of the FileStream to perform the disk operation.

  • Use File.ReadAllText to read the file back into a string in Program.cs.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • Remember that the FileStream constructor takes the file name and a FileMode enum value.

  • To create the byte array, you need to access Encoding.UTF8.GetBytes(yourString).

  • The Write method requires three arguments: the byte array, the offset (which is usually 0), and the total number of bytes to write (the array’s Length).

  • You can quickly verify the file in Program.cs by passing the logFile variable into File.ReadAllText().

C# 14.0
using System.IO;
using System.Text;
namespace CodingExercise;
public static class WeatherUtility
{
public static void SaveTemperatures(string dailyTemperatures, string fileName)
{
// Add your file writing code below this line
}
}