University of Washington Computer Programming I

Slides:



Advertisements
Similar presentations
File Management in C. What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary.
Advertisements

BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
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.
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
C Programming - Lecture 3 File handling in C - opening and closing. Reading from and writing to files. Special file streams stdin, stdout & stderr. How.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
1 ICS103 Programming in C Lecture 8: Data Files. 2 Outline Why data files? Declaring FILE pointer variables Opening data files for input/output Scanning.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
B-1 Lecture 2: Problems, Algorithms, and Programs © 2000 UW CSE University of Washington Computer Programming I.
Guide To UNIX Using Linux Third Edition
CSE1301 Computer Programming: Revision. Topics Type of questions What do you need to know? About the exam Exam technique Sample questions.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
V-1 University of Washington Computer Programming I File Input/Output © 2000 UW CSE.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
Files COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
C Basic File Input/Output Manipulation C Programming – File Outline v File handling in C - opening and closing. v Reading from and writing to files.
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.
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
Chapter 11 File Processing. Objectives In this chapter, you will learn: –To be able to create, read, write and update files. –To become familiar with.
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
Chapter 7 : File Processing1 File-Oriented Input & Output CHAPTER 7.
1 CHAPTER6 CHAPTER 6. Objectives: You’ll learn about;  Introduction  Files and streams  Creating a sequential access file  Reading data from a sequential.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
E-1 University of Washington Computer Programming I Lecture 5: Input and Output (I/O) © 2000 UW CSE.
9/29/99B-1 CSE / ENGR 142 Programming I Variables, Values, and Types © 1998 UW CSE.
G3-1 University of Washington Computer Programming I Structuring Program Files © 2000 UW CSE.
Q-1 11/29/98 CSE / ENGR 142 Programming I Input/Output, Libraries, and Files © 1998 UW CSE.
Files A collection of related data treated as a unit. Two types Text
CSCI N305: C Programming Copyright ©2006  Department of Computer & Information Science File Handling in C.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
Lecture 20: C File Processing. Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program terminates. Files are used.
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
User-Written Functions
Lesson #2 Introduction to C.
Lesson #2 Introduction to C.
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
TMF1414 Introduction to Programming
Chapter 7 Text Input/Output Objectives
File Access (7.5) CSE 2031 Fall July 2018.
File I/O.
Lecture 13 & 14.
File Input/Output.
Programming in C Input / Output.
Programming in C Input / Output.
Files I/O, Streams, I/O Redirection, Reading with fscanf
Chapter 11 – File Processing
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
FILE HANDLING IN C.
Text and Binary File Processing
File Input and Output.
File Handling.
Fundamental of Programming (C)
Programming in C Input / Output.
Lesson #2 Introduction to C.
File Handling in C.
Chapter 12: File I/O.
EPSII 59:006 Spring 2004.
Module 12 Input and Output
ETE 132 Lecture 8 By Munirul Haque.
Chapter 11 Files chap8.
C Programming - Lecture 3
CSc 352 File I/O Saumya Debray Dept. of Computer Science
EECE.2160 ECE Application Programming
ICS103: Programming in C 6: Pointers and Modular Programming
Presentation transcript:

University of Washington Computer Programming I File Input/Output Su00 tvi: rearrange to concentrate on file I/O only. Add in material about result of scanf - using it to see what happened, control loops that do file I/O © 2000 UW CSE

Overview Concepts External disk files Opening files for reading/writing File variables File I/O Closing Files

Review: What is I/O? Monitor Central Processing Unit Disk (Files) Main Point of this is that scanf/printf transfer data between memory and the outside world (keyboard/monitor) Main Memory mouse Keyboard Network

Files A "file" is a collection of data on disk Su00 tvi: Deleted reference to DOS 8.3 file names

Why Files? Large volume of input data Large volume of output data More permanent storage of data Transfer to other programs Multiple simultaneous input and/or output streams

DATA FILES Business Data: customer files, payroll files, … Scientific Data: weather data, environmental data, topographic maps, … Image Data: web images, satellite images, medical images, … Web Data: HTML, GIF, JPEG, PNG, XML, … Su00 tvi: deleted some odd slides here

Files A "file" is a collection of data on disk managed by the user and the operating system Permanent A "file name" is how the user and OS know the file follows OS naming rules We'll look at using text files in a C program Su00 tvi: Deleted reference to DOS 8.3 file names

Files as Streams of Characters keyboard/screen are special cases input / output streams of characters Multiple streams can be used simultaneously In reality, stream flows through a buffer rather than directly into or out of variables. error\n abc12 12 program variables hx119 8s 12, 13, 13

Files vs. File Variables Key idea A file is a collection of data on disk For our purposes, a sequence of characters But a C program can only operate directly on variables (data in main memory), so… Need to make a connection between the data on the disk and variables in main memory Su00 tvi: there’s probably a better way to say this

Files vs. File Variables A file variable is a data structure in the C program which represents the file Temporary: exists only when program runs There is a struct called FILE in <stdio.h> Details of the struct are private to the standard C I/O library routines Su00 tvi: there’s probably a better way to say this

What's in stdio.h? Prototypes for I/O functions. Definitions of useful #define constants Example: EOF for End of File Definition of FILE struct to represent information about open files. File variables in C programs are pointers to a FILE struct. FILE *myfile;

Opening A File "Opening" a file: making a connection between the operating system (file name) and the C program (file variable) Files must be opened before they can be used

Computer System Monitor Central Processing Unit Disk (Files) Main Point of this is that scanf/printf transfer data between memory and the outside world (keyboard/monitor) Main Memory mouse Keyboard Network

Opening A File To open a disk file in C: library function fopen specify "r" (read, input) or "w" (write, output) NB String “r”, not char ‘r’ ! Files stdin/stdout (used by scanf/printf) are automatically opened & connected to the keyboard and display

File Open Example /*usually done only once in a program*/ /*usually done near beginning of program*/ FILE *infilep, *outfilep; /*file variables*/ char ch; /* Open input and output files */ infilep = fopen (“Student_Data”, “r” ) ; outfilep = fopen (“New_Student_Data”, “w”) ;

File I/O: fscanf and fprintf Once a file has been opened... use fscanf and fprintf to read or write data from/to the file Use the file variable returned by fopen to identify the file to be read/written File must already be open before fscanf or fprintf is used!

File I/O: fscanf and fprintf

File I/O: fscanf and fprintf fscanf: works just like scanf, but 1st parameter is a file variable fscanf (filepi, “%...”, &var, ... ) ; fprintf: works just printf, but 1st parameter is a file variable fprintf (filepo, “%...”, var, ... ) ;

Copying a File "If you really uderstand this example. you understand enough for this course."

Copying a File /* Copy a file one character at a time */ /* files must already be open before this*/ fscanf (infilep, “%c”, &ch); while ( “we just read a character” ) { fprintf (outfilep, “%c”, ch) ; } /* all done */ Question: How do we tell when we’ve read the last character from the input file? "If you really uderstand this example. you understand enough for this course."

Detour: (f)scanf return value

Detour: (f)scanf return value Both scanf and fscanf are really int-valued functions Besides storing input values in variables, they return a status code: Tells the number of values successfully read Can be used to see if the number of values read is the number expected. If not, there must have been an error. Can also be used to detect when end of file is reached

scanf Old Style int status, id, score ; double grade ; scanf(“%d %lf %d”, &id, &grade, &score) ; Problem: how would you know if there was an error in input (garbage typed when number expected, etc.)?

scanf with return value int status, id, score ; double grade ; status = scanf(“%d %lf %d”, &id, &grade, &score) ; if (status < 3) printf(“Error in input \n”) ; This is how you can detect errors Don’t confuse status (function value) with input (variables in parameter list)

End of File (EOF) Status EOF: a special status value Returned by scanf and fscanf when end of data is reached defined in stdio.h #define EOF (some negative value) I/O library routines use EOF in various ways to signal end of file. Your programs can check for EOF EOF is a status, not an input value!! Sometimes even advanced programmers think of EOF as a value read from the file, rather than a status. The latter view should be stressed. Removed reference to exact value of EOF (often -1, but programmers shouldn’t be thinking of that or depending on it)

File Copy Example, Concluded /* Copy a file one char at a time until EOF*/ /* files must already be open before this*/ status = fscanf (infilep, “%c”, &ch); while ( status != EOF ) { fprintf (outfilep, “%c”, ch) ; } /* all done */ "If you really understand this example. you understand enough for this course."

Closing A File Breaks the link between file variable and file Usually done only once in a program, near end of program Closing an output file is essential, or data may be lost! infilep = fopen (“Student_Data”, “r” ) ; .../*process the file */ ... /*when completely done with the file:*/ fclose (infilep); Su00 tvi: Moved this here so discussion in the lecture follows the sequence of steps needed to process a file: open, read/write, close

Review: Essential Functions for Text File I/O

Review: Essential Functions for Text File I/O fopen and fclose fscanf: status = fscanf (filepi, “%...”, &var, ... ) ; /* fscanf returns EOF on end of file */ fprintf: fprintf (filepo, “%...”, var, ... ) ; File must already be open before before fscanf or fprintf is used!

Building Applications with Files With fopen, fclose, fprintf, and fscanf you can write lots of useful programs involving files Many errors and exceptions can arise when using files A robust program must handle errors scanf's return value informs us of errors Su00 tvi: Deleted larger (tiny font) examples after this. No idea how one could handwave through them on TV

Summary Files are collections of data on disk A file must be opened be used A file should be closed after use fprintf is used to write text files fscanf is used to read text files Su00 tvi: Deleted larger (tiny font) examples after this. No idea how one could handwave through them on TV