Reading and writing to text files.
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:
The new commands used in this program and others that may be useful. Select them below to learn more:
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;
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;
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.
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.
Writes a new line of data y to an open file x.
Writes data y to an open file x. Data is appended without a new line.
Close the file pointer x. File should be closed as soon as possible. Not closing files can in some cases corrupt the file.
Questions to think about with this program to check your understanding:
Identify a hidden character in the file `datafile.txt` that will probably be invisible when looking at the file.
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.
Explain the function of user.Trim(); in line 15.
.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.
Change the program so that:
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.
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.
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.