Intro to Computer Science CS1510 Dr. Sarah Diesburg

Slides:



Advertisements
Similar presentations
File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
Advertisements

Why use Files? Your Python Program External File (secondary storage) Write to file (Save) Read from file (Load) …so we can have access to ‘stored’ data.
Files and Streams. Goals To be able to read and write text files To be able to read and write text files To become familiar with the concepts of text.
File I/O There’s more to life than the keyboard. Interactive vs. file I/O All of the programs we have seen or written thus far have assumed interaction.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Introduction to Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
File I/O Static void Main( ) {... } data. Topics I/O Streams Reading and Writing Text Files Formatting Text Files Handling Stream Errors File Pointers.
Numerical Representation Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
1 CS161 Introduction to Computer Science Topic #13.
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
Lab 07: Caesar Cypher Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
File I/O Ruth Anderson UW CSE 160 Spring File Input and Output As a programmer, when would one use a file? As a programmer, what does one do with.
Chapter 5 Files and Exceptions I. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a file? A.
Files Tutor: You will need ….
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
FILES. open() The open() function takes a filename and path as input and returns a file object. file object = open(file_name [, access_mode][, buffering])
CSV Files Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Introduction to Files Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
Python focus – files The open keyword returns a file object Opening a file myFile = open('C:\file.txt', arg) Optional argument The second argument controls.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Chapter 14: Sequential Access Files
CSC201: Computer Programming
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
Chapter 8 Text Files We have, up to now, been storing data only in the variables and data structures of programs. However, such data is not available.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CSC 131: Introduction to Computer Science
Numerical Representation
Chapter 7 Text Input/Output Objectives
File I/O.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Ruth Anderson UW CSE 160 Winter 2016
Thinking about programming
Ruth Anderson UW CSE 160 Winter 2017
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Seek Method and CSV Files
Ruth Anderson UW CSE 160 Spring 2018
Topics Introduction to File Input and Output
String Encodings and Penny Math
File IO and Strings CIS 40 – Introduction to Programming in Python
Fundamentals of Programming I Number Systems
Using files Taken from notes by Dr. Neil Moore
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Seek Method and CSV Files
Thinking about Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
COMP 101 Introduction.
Input and Output with FILES
Numerical Representation
Files Handling In today’s lesson we will look at:
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Introduction to File Input and Output
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
String Encodings and Penny Math
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Introduction to File Input and Output
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Numerical Representation
Presentation transcript:

Intro to Computer Science CS1510 Dr. Sarah Diesburg Introduction to Files Intro to Computer Science CS1510 Dr. Sarah Diesburg

Penny Math Penny Math is a simple formula Thus A (or a) costs 1 penny B (or b) costs 2 pennies … Z (or z) costs 26 pennies Everything else is FREE Thus “Sarah” costs 19+1+18+1+8=47 cents “Diesburg” costs 4+9+5+19+2+21+18+7=85 cents “Sarah Diesburg!!!” costs 47+85=132 cents, or $1.32 “@#$!!” is free

Dollar Words “Dollar words” are words that cost EXACTLY $1 (100 cents) Suppose I told everyone I would give you extra credit if you could find more dollar words than anyone else in the class.

Dollar Words How to go about that? Who wants to search around dreaming up words and testing their cost??? But what if I had a dictionary file?

What is a File? A file is a collection of data that is stored on secondary storage like a disk or a thumb drive. Accessing a file means establishing a connection between the file and the program and moving data between the two.

Two Types of Files Files come in two general types: Text files: files where control characters such as “\n” are translated. These are generally human readable Binary files: all the information is taken directly without translation. Not readable and contains non-readable info.

File Objects or Stream When opening a file, you create a file object or file stream that is a connection between the file information on disk and the program. The stream contains a “buffer” of the information from the file, and provides the information to the program

Making a File Object fileObject = open(“myFile.txt”, “r”) myFile is the file object. It contains the buffer of information. The open function creates the connection between the disk file and the file object. The first quoted string is the file name on disk, the second is the mode to open it (here,“r” means to read).

Where is the Disk File? When opened, the name of the file can come in one of two forms: “file.txt” assumes the file name is file.txt, and it is located in the current program directory. “c:\bill\file.txt” is the fully qualified file name and includes the directory information.

Different Modes “r” is to read as a text file. “w” is to write as a text file. Wipes the contents of the file if there is any, creates file otherwise. “a” is append, adds to the end of an existing file. There are others, if you are curious. These are the only 3 you need to know for the rest of this class.

Be Careful with Write Modes Be careful if you open a file with the ‘w’ mode. It sets an existing file’s contents to be empty, destroying any existing data. The ‘a’ mode is nicer, allowing you to write to the end of an existing file without changing the existing contents.

Text Files use Strings If you are interacting with text files (which is all we will do for this semester), remember that everything is a string: everything read is a string if you write to a file, you can only write a string

Getting File Contents Once you have a file object: fileObject.read() Reads the entire contents of the file as a string and returns it. It can take an optional argument integer to limit the read to N bytes, that is fileObject.read(N). fileObject.readline() Delivers the next line as a string.

More File Contents fileObject.readLines() for line in fileObject: Returns a single list of all the lines from the file. for line in fileObject: Iterator to go through the lines of a file.

Writing Once opened, you can write to a file (if the mode is appropriate): fileObject.write(s) writes the string s to the file fileObject.writelines(list) write a list of strings (one at a time) to the file

Closing When done, you close the file. Closing is important because the information in the fileObject buffer is “flushed” out of the buffer and into the file on disk, making sure that no information is lost. fileObject.close()

Back to Penny Math Let’s look at how I can use a dictionary file to compute dollar words…