Exercise: Weather Log Writer
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 theSaveTemperaturesmethod.Convert the provided
dailyTemperaturesstring into a UTF-8 encoded byte array.Save the bytes to the file path provided by the
fileNameparameter.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.GetBytesmethod to convert the string.Instantiate a
FileStreamwithFileMode.Create.Use a modern
usingdeclaration (using var) to ensure the file stream is safely disposed of without using curly braces.Use the synchronous
Writemethod of theFileStreamto perform the disk operation.Use
File.ReadAllTextto read the file back into a string inProgram.cs.
Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.
Get hints
Remember that the
FileStreamconstructor takes the file name and aFileModeenum value.To create the byte array, you need to access
Encoding.UTF8.GetBytes(yourString).The
Writemethod requires three arguments: the byte array, the offset (which is usually0), and the total number of bytes to write (the array’sLength).You can quickly verify the file in
Program.csby passing thelogFilevariable intoFile.ReadAllText().
Exercise: Weather Log Writer
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 theSaveTemperaturesmethod.Convert the provided
dailyTemperaturesstring into a UTF-8 encoded byte array.Save the bytes to the file path provided by the
fileNameparameter.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.GetBytesmethod to convert the string.Instantiate a
FileStreamwithFileMode.Create.Use a modern
usingdeclaration (using var) to ensure the file stream is safely disposed of without using curly braces.Use the synchronous
Writemethod of theFileStreamto perform the disk operation.Use
File.ReadAllTextto read the file back into a string inProgram.cs.
Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.
Get hints
Remember that the
FileStreamconstructor takes the file name and aFileModeenum value.To create the byte array, you need to access
Encoding.UTF8.GetBytes(yourString).The
Writemethod requires three arguments: the byte array, the offset (which is usually0), and the total number of bytes to write (the array’sLength).You can quickly verify the file in
Program.csby passing thelogFilevariable intoFile.ReadAllText().