Saving data

Saving data

Reading and writing to text files.

Try


Select the button below to open the C# program in a new window. Run the program and read the lines of code to see if you can understand how it works. It will be helpful to arrange your display so that you can have this browser window on one side of the screen and the code on the other.

Watch this video to learn about the new concepts shown in the program:

Knowledge Organiser

Command summary

The new commands used in this program and others that may be useful. Select them below to learn more:

StreamReader x = new StreamReader(y);

x is a pointer to the file y.

This command is used to open a file for reading. The file must already exist otherwise the program will crash.

Note that a file can only be open for either reading (using a StreamReader) or writing (using a StreamWriter) at any one time.

You must make sure the System.IO namespace is included in your program before attempting to open a file:

using System.IO;

StreamWriter x = new StreamWriter(y, z);

x is a pointer to the file y with the data flow z. The data flow can be:

true - append data to the end of the file;

false - overwrite existing data in the file.

If the parameter z is not specified existing data will be over-written.

If the file does not already exist, then it will be created when the StreamWriter is assigned.

Note that a file can only be open for either reading (using a StreamReader) or writing (using a StreamWriter) at any one time.

You must make sure the System.IO namespace is included in your program before attempting to open a file:

using System.IO;

string x = y.ReadLine();

x is assigned to one line of data from an open file y.

Note data may include character codes, for example end of line characters. Use the `.Trim()` method to remove invisible character codes from the data read from a file.

string x = y.ReadToEnd();

x is assigned to all the data from an open file y.

Note data may include character codes, for example end of line characters. Use the `.Trim()` method to remove invisible character codes from the data read from a file.

x.WriteLine(y);

Writes a new line of data y to an open file x.

x.Write(y);

Writes data y to an open file x. Data is appended without a new line.

x.Close()

Close the file pointer x. File should be closed as soon as possible. Not closing files can in some cases corrupt the file.

Investigate


Questions to think about with this program to check your understanding:

Item question

Identify a hidden character in the file `datafile.txt` that will probably be invisible when looking at the file.

REVEAL ANSWER

Carriage return (CR) or Line feed (LF) and End of file marker (EoF) are all hidden characters that are placed into the file by the program or operating system. They enable the computer to know how to display the data in the file to the user. I.e. where a new line should start and where the data in the file ends.

Structure question

Explain the function of user.Trim(); in line 15.

REVEAL ANSWER

.Trim() is a method that removes the hidden characters from the variable holding the contents of file. Although these are read into memory from the file a user doesn't want to see them.

Make


Success Criteria

Change the program so that:

  1. If a user has been stored in the file it outputs, "It is good to see you again," followed by the first line of data in the file.
  2. If the datafile.txt is empty it outputs, "Hello, I don't think we have met.", asks the user for their name and saves this to datafile.txt.

Typical inputs and outputs from the program would be:

Hello, I don't believe we have met.

What is your name? Dave

Nice to meet you Dave.


It's good to see you again, Dave.


Help

Use the flowcharts if you need more support in understanding the steps required to meet the criteria. Use the Parsons code sorting exercise only if you are really stuck.

Evaluate


Check your program works by removing the data in the datafile.txt file and running the program. Note, do not delete the file otherwise the program will not work.

Run the program for a second time to verify the data was saved and loaded correctly.

Registered in England and Wales: 10442992

VAT Number: 290 9845 58

Telephone: 01452 947500

Email: admin@craigndave.co.uk

Bett Awards 2024 Finalist