File Handling Programming Tasks
ONLY upload your HeAD with Code copied as text and Screenshots of console windows with the code running
the final chapter of Introduction to Programming in VB.net has some good examples and a list of the file procedures you will require. Where the problems refer to a pre-prepared file, it means that you have created a file in notepad that has suitable contents for testing. Problems without chips are compulsory. You also need to do 6 chips worth of problems.
Whilst we don't want to be too dogmatic about how you should work with files, the methods in the workbook examples are the same as the exam board use in Comp1 exams. If you choose to use better methods you risk not being as ready for the exam as others.
List of useful function (search msdn for examples of how to use them
FileClose(#) |
It is important to close files after you have finished using them. If you don’t then the operating system will still this it is being used |
EOF(#) |
A Boolean function that returns true when the end of a file is reached. Useful when looping through reading a file. |
Input(#,myStr) |
Reads a single data item that has been delimited by commas and quotes. Passes it to the second parameter (called myStr in the example) |
LineInput(#) |
A function that returns a whole line of a file at a time. You need to assign it to a variable. |
InputString(#,x) |
Unlike the Input function, the InputString function returns all of the characters it reads, including commas, carriage returns, line feeds, quotation marks, and leading spaces, (x is a specific number of characters) |
Print(#,myStr) |
Writes display-formatted data to a sequential file |
Printline(#,myStr) |
Same as Print but adds a line feed at the end |
Write(#,myStr) |
function inserts commas between items and quotation marks around strings as they are written to the file |
Writeline(#,myStr) |
Same as Write but adds a line feed at the end |
LOC (#) |
Function returns current location within a file |
LOF(#) |
Function returns the length of a file |
TAB(x) or SPC(x) |
Used with print or write to add a number of tabs or spaces |
StreamReader Methods
Closes the StreamReader object and the underlying stream, and releases any system resources associated with the reader. |
|
Releases all resources used by the TextReader object. (Inherited from TextReader) |
|
Returns the next available character but does not consume it. |
|
Reads the next character from the input stream and advances the character position by one character. |
|
Reads a specified maximum number of characters from the current stream and writes the data to a buffer, beginning at the specified index. |
|
Reads a line of characters from the current stream and returns the data as a string. |
|
Reads all characters from the current position to the end of the stream. |
|
Returns a string that represents the current object. (Inherited from Object) |
Reading from text files of a known length
For the following programs make sure that:
- A pre-Prepeared file is opened (see instructions above)
- File is opened
- the contents of the file are assigned to variable, then outputted to the console
- the file is closed
- Copy and paste your code into a head, screen shot of the program running with the file you created also showing
- Create a file that has 5 lines of text on 5 seperate lines. Create a program that reads and displays the 5 lines of text
- You should use an array to hold the content of the file
- Create a file that has 5 items of text seperated by commas. Create a program that reads and displays the 5 items of text on separate lines in the console
- Make sure your file is comma delimited and the output isn't
- Adding up numbers from a file
- Create a file with 10 numbers (one on each line)
- Write a program that displays the numbers and a final line that clearly displays the sum
- Extra marks for the nicest display of this sum
- No need to use an array for this one
- Adding up numbers from a file
- Create a file with 10 numbers (ON SINGLE LINE SEPARATED BY COMMAS)
- Write a program that displays the numbers and a final line that clearly displays the sum
- Extra marks for the nicest display of this sum
- No need to use an array for this one
Reading text files of an unknown length
6. How many lines?
- Create a file with several lines of txt.
- write a program that outputs the contents of the file and displays how many lines there are
7. Adding up numbers from a file
- Create a file with an unknown number of numbers (one on each line)
- Write a program that displays the numbers and a final line that clearly displays the sum
- Extra marks for the nicest display of this sum
- Put your code and a screen shot of the program running in the HeAD
- NOW try with an unknown number of numbers on a single line separated by commas
Writing to text files
- Write a single line of text to a file
- User inputs a line of text into console
- A pre-Prepeared file is opened
- the users input is writen to the first line of the file
- File is closed
2. Write a single line of text to the end of a file (filemode=append)
- User inputs a line of text into console
- A pre-Prepeared file is opened
- the users input is writen to a new line at the end of a file
- File is closed
Writing Binary Files using a Structure (user defined data type)
Create a structure with the following fields: Name, Price, Cost. Add a five records (lines) either hard-coded or input from console(make sure the price is more than the cost!) Both price and cost should be in the data format ##.##. You will need to use the Put function see this example
The FileOpen method starts to creak at this point.. You may be better to switch to the BinaryReader/BinaryWriter classes. You should be able to use MSDN by now to work out how to use these techniques https://docs.microsoft.com/en-us/dotnet/api/system.io?view=netframework-4.7.2
Reading into a structure from a file.(user defined data type)
Create a file with the following dataheaders: Name, Price, Cost. Add a five records (lines) separating the fields by commas.(make sure the price is more than the cost!) Both price and cost should be in the data format ##.##.
Name | Price | Cost |
Freddo | 25 | 15 |
Mars | 75 | 50 |
Apple | 50 | 40 |
Pear | 100 | 70 |
cookie | 150 | 25 |
8. Simple Reading.
- Create a custom datatype record (structure) called: Product
- Add three fields (variables) called: Name,Price,Cost
- Write some code that will read the file you created and display the contents nicely
9. Profit margin
- Using the code and file from question 8.
- find the profit margin for each record (price-cost) (not the headers!)
- Display this information in the console.
Fun Problems (worth chips!)
10 Find a word (2 chips)
create a program that finds if a word you input is located in a named text file. extra chip for the line number of the first instance. extra 2 chips if you ouput the line number of each instance to the console
11. Find and Replace (4chips)
Create a program that opens a file and replace every instance of one word with another (e.g. "student" for "chimp"). Create a txt file (just copy a wiki article into a txt file). there are lots of ways to solve this problem. The nicest would be to just change the words in the original file. not easy! (especially using the fileOpen method). creating a new file with the altered text is straight forward. you can even cheat and use a method called .replace and work on the file a line at a time).
- open 2 files (1 read, 1 write)
- read each line
- string.replace
- write newline to new file
- close both both files
I will award extra chips for more elegant solutions
12. File Stats.
Create a program that give the statistics for the number of occurrences of each alphabetic characters in a text file. (3 chips). An extra 2 chips if you also add a top five words feature. a simple output to the console would be fine. appending to the bottom of the file would be worth an extra chip.