Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamentals of Data Structures

Similar presentations


Presentation on theme: "Fundamentals of Data Structures"— Presentation transcript:

1 Fundamentals of Data Structures
File Handling

2 Fundamentals of Data Structures: File Handling
One very important aspect of a program is its ability to permanently save data to a file This means file handling I.e. outputting to a file, and getting input from a file There are two terms for this: Output to a file: Save Input from a file: Load In either case, we first need to open the file and perform the correct task on it Once we’re done, we need to close the file Keeping a file open locks it by this program That means no other program can open it in the meantime Fundamentals of Data Structures: File Handling

3 Fundamentals of Data Structures: File Handling
Whenever we open a file, we typically state why we’re opening the file As in, what mode are we opening the file in We have three different modes when opening a file Write: writing (and overwriting) data to the file Append: writing (adding near the bottom) data to the file Reading: reading data from the file We also have two possibilities when it comes to writing to a file The file doesn’t exist (we’re making it right now) The file does exist (we’re overwriting it, or adding to it) Fundamentals of Data Structures: File Handling

4 File Handling: Creating Files
The first possibility we will deal with is saving data to a file But no file has been made so far What we’re essentially doing here is making a new file at the same time This means we need to have the data in a format ahead of time Usually text-based However, will also look at CSV (Comma Separated Values) Fundamentals of Data Structures: File Handling

5 File Handling: Creating Files
Here’s an example of creating a file and writing some text to it Python Java Note that only Python has a mode character (‘w’ for ‘write’). In C# and Java, the mode is represented by the StreamWriter and the FileWriter. C# Fundamentals of Data Structures: File Handling

6 Fundamentals of Data Structures: File Handling
Create a new program which does the following Asks for the user’s name and age Creates a new text file and saves both of these to it If we run this program multiple times, it’ll simply overwrite the content that was in it originally Run your program multiple times, checking the text file after each run! Fundamentals of Data Structures: File Handling

7 File Handling: Appending to Files
If we want to repeatedly add to a file, then we don’t use the write mode We use the append mode For example, we could make a program which logs the user’s name Each time the program is run Writing would overwrite this log every time, so we need to use append Fundamentals of Data Structures: File Handling

8 File Handling: Appending to Files
Here’s an example of creating a file and writing some text to it Python Java For Python, we change the “w” to “a”. For C#, we add true when making the StreamWriter. For Java, we replace the write() call with append(). Note that this still works even if the file doesn’t exist yet! C# Fundamentals of Data Structures: File Handling

9 Fundamentals of Data Structures: File Handling
Change your program from the previous exercise so that it appends to the file instead! Don’t forget to add a newline character at some point Otherwise the text in the file won’t move on to the next line Fundamentals of Data Structures: File Handling

10 File Handling: Reading from a File
Finally, we need to see how we can read from a file Could be a save file for a game Or the name of the user who previously used the program In Python, we switch the mode to r (for read) and use a different function For C# and Java, we need to use different objects Readers rather than Writers Fundamentals of Data Structures: File Handling

11 File Handling: Reading from a File
Here’s how we read from a file Python Java Python and C# have simple ways of reading all the content from a file. In Java, however, we read it a line at a time. This is using the Scanner (the same thing we use to read input from the console)! C# Fundamentals of Data Structures: File Handling

12 Fundamentals of Data Structures: File Handling
In the same program you made earlier (after append data to the file) Read the content from the file and output it to the console In Python and C#, you can use a simple function for reading all the content In Java, you need to read the file one line at a time Fundamentals of Data Structures: File Handling

13 Plaintext and CSV Files
Everything we’ve written to so far has been plaintext The file contains only ASCII characters With no other formatting However, sometimes we want to store information in a bit more of a structured way For example, we want to store a table (with columns and rows) A plaintext file isn’t suited to that So then what is? Fundamentals of Data Structures: File Handling

14 Fundamentals of Data Structures: File Handling
CSV Files That’s where Comma Separated Value (CSV) files come in! They are still plaintext based, but they have a bit more formatting In CSV files, we separate different columns using commas And different rows using newlines The great thing is that CSV files are supported by a lot of table management software Like Microsoft Excel! Fundamentals of Data Structures: File Handling

15 Fundamentals of Data Structures: File Handling
CSV Files For example, we may be trying to store high scores in a video game We need to remember the user’s name, their score, and the date they got that score on We can store these values for each user on different lines Data in CSV file Opened in Excel Name Score Date Jason 123000 02/05/2026 Emily 101000 04/01/2026 Sarah 98000 15/03/2026 Name,Score,Date Jason,123000,02/05/2026 Emily,101000,04/01/2026 Sarah,98000,15/03/2026 Fundamentals of Data Structures: File Handling

16 Fundamentals of Data Structures: File Handling
CSV Files The trick with saving CSV files is to add a comma after every attribute And a newline after every record However, we also need to read/load them differently As we need to read them one line at a time (for each record) Then split that line based on the commas (for each attribute) Sometimes, when saving a CSV file, a blank newline can be found at the end We will also need to stop this from happening It could crash the program when loading (as we try and split an empty string) Fundamentals of Data Structures: File Handling

17 Fundamentals of Data Structures: File Handling
CSV Files: Saving Here’s an example of saving to a CSV file In Java, when writing using a FileWriter, we need to convert any numbers into a String Otherwise it thinks of the number in ASCII/Unicode Java Fundamentals of Data Structures: File Handling

18 Fundamentals of Data Structures: File Handling
CSV Files: Saving Here’s an example of saving to a CSV file C# Fundamentals of Data Structures: File Handling

19 Fundamentals of Data Structures: File Handling
CSV Files: Saving Here’s an example of saving to a CSV file Python Fundamentals of Data Structures: File Handling

20 Fundamentals of Data Structures: File Handling
CSV File: Loading Java And here’s an example of loading a CSV file Here we’re working through the file twice Once to work out how many lines there are (for the array sizes) Twice to move the values in the file into the correct place in the array Fundamentals of Data Structures: File Handling

21 Fundamentals of Data Structures: File Handling
CSV File: Loading And here’s an example of loading a CSV file C# Fundamentals of Data Structures: File Handling

22 Fundamentals of Data Structures: File Handling
CSV Files: Saving And here’s an example of loading a CSV file As Python uses lists for everything, we don’t need to read through the file twice We simply add the attributes to the lists as needed Python Fundamentals of Data Structures: File Handling

23 Fundamentals of Data Structures: File Handling
Create a CSV file (programmatically) that contains values for Name (string) Test Result (integer between 0 and 100) Grade (string from F to A, though you can decide what result gets what grade) Make at least three records with each of these attributes and write them to the CSV file Make a procedure to do this Make another procedure for reading from this CSV file And storing the results into three arrays/lists You may need to make these arrays global to get their values out of the procedure Fundamentals of Data Structures: File Handling

24 Fundamentals of Data Structures: File Handling
Binary Files The only other file type we would need to think about are binary files These aren’t text-based files Like plaintext or CSV files They are the most pure file types, as they only contain binary data 1s and 0s! When writing/reading with these file-types, we need to encode/decode whatever data we’re dealing with Fundamentals of Data Structures: File Handling

25 Fundamentals of Data Structures: File Handling
Binary Files We’re going to make a string value and get its binary data In the form of a byte array Then we’ll write this information to file Using a different file-extension than usual We’ll make our own – called Binary Data File (.bdf) Fundamentals of Data Structures: File Handling

26 Binary Files: Writing Here’s an example of writing a binary data file
Python There are a few changes here In all three languages, we need to convert the string into a byte array (sometimes specifying the encoding to use, like UTF-8). In Python, we use the usual method except for the added “b” in the open mode (which means binary). In Java and C#, we can use a separate method for writing binary. Java C# Fundamentals of Data Structures: File Handling

27 Fundamentals of Data Structures: File Handling
Binary Files: Writing And here’s reading byte data from a file Python Java C# Fundamentals of Data Structures: File Handling

28 Fundamentals of Data Structures: File Handling
Make a string phrase and convert it to a byte array Write this byte array to a file Using a file-extension of your choice Run this program, and try opening the file with a text-editor Can you read the data in it? Now try reading the data from the file into a byte array Convert it into a string Then output it to the console Fundamentals of Data Structures: File Handling

29


Download ppt "Fundamentals of Data Structures"

Similar presentations


Ads by Google