1 / 20

Introduction to Files

Introduction to Files. CSIS 1595: Fundamentals of Programming and P roblem Solving 1. Files and Programs. Variables act as “link” to files Files can be opened in read , write , or append mode Write overwrites old file, append adds to end Text files consist of “string” of characters

chiko
Download Presentation

Introduction to Files

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Files CSIS 1595: Fundamentals of Programming and Problem Solving 1

  2. Files and Programs • Variables act as “link” to files • Files can be opened in read, write, or append mode • Write overwrites old file, append adds to end • Text files consist of “string” of characters • Characters read/written one at a time • Variable maintains “position” of program in file variable

  3. Files and the OS • Files accessed via operating system • Operating system often buffers I/O for efficiency • Writes multiple characters to file once instead of one at a time when statement executed (efficiency) Program Input variable Output variable Operating System <<< data <<< >>> data >>> buffered data

  4. Opening Files • Must open file before use • Get link to file via operating system • Syntax: variable = open(“filename”, mode) • By default in same directory as program • Mode: • “r”: read from file • “w”: write to file (overwriting previous contents • “a”: append to file (adding to end of previous contents)

  5. Reading from Files • Simplest method: Use for to iterate through filefor stringvariable in filevariable:

  6. Reading from Files • Each time through loop stringvariable assigned next entire line in file • All characters up to (and including) the \n • Often use strip function to remove extra \n from end Second time through loop Third time through loop First time through loop line

  7. Writing to Files • Simplest method: Use modified form of print statementprint(string, file=“filevariable”) • Writes the string to the file (with \n at end) • Each print adds to end of last print

  8. Closing Files • Should close file after using • Returns control of file to OS • Forces OS to write anything in buffer to file • Otherwise, no everything may actually be written to file! • Syntax: filevariable.close()

  9. File Reading and Line Parsing • Often easier for humans to read file if multiple pieces of data on same line • Program must read in entire line as string • Then split line into component parts (split method) • Key: File must use some character to indicate split not used as part of data • Example: Comma Separated Values (CSV)

  10. File Reading and Line Parsing

  11. Program Design and Files • Common uses: • Processing contents of file to create result • Example: Computing sum of all numbers in file • May involve editing file in some way • Example: Removing duplicate names in file • Saving state of program • When program stopped, vital information saved • When program restarted, that information read back into variables • Example: Managing appointment list

  12. Example: Computing Totals • Input: numbers.txt, containing floats • Possibly several per line separated by spaces • Goal: Print total of numbers in file • Algorithm: • Read each line into string, parse into list • Iterate through list, add to running total

  13. Example: Computing Totals

  14. Example: Removing Duplicates • Input: names.txt, containing names (one per line) • Output: no_duplicates.txt, containing same names with duplicates removed • Algorithm: • Keep list (initially empty) of all names read • For each name read in: • Use count to see if already in list (count of 0) • If not, add to list and write to file

  15. Example: Removing Duplicates

  16. State Storage • Goal: Store crucial program data between runs so user may “pick up” where they left off • Games • Text editors… • When program ends, store all necessary variables in file (probably one per line for simplicity) • Might also do after every change • Might also give user “save” command • When program restarts, read data from that file and store into those same variables • Might let user choose file

  17. Appointment Book Example • Maintain list of “appointments” for times 0 – 9 • Create new appointment at a given time • Cancelappointment at a given time • Printall appointments • Internal representation: list • Must be able to save appointments between runs of the program • Prompt user for name of file at start • Write modified appointments at end to same file

  18. Appointment Book Example • Often implement file read/write as functions • Call at beginning/end/when requested by user

  19. Appointment Book Example • Key questions: • What program data must be stored  List of appointments • How to store it  One name per line in order

  20. Appointment Book Example • Key questions: • What program data must be stored  List of appointments • How to store it  One name per line in order

More Related