Topic 8: I/O. Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional)

Similar documents
File Access. FILE * fopen(const char *name, const char * mode);

Standard File Pointers

Input / Output Functions

Accessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

File IO and command line input CSE 2451

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

Computer Programming: Skills & Concepts (CP1) Files in C. 18th November, 2010

Computer Security. Robust and secure programming in C. Marius Minea. 12 October 2017

C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

25.2 Opening and Closing a File

Mode Meaning r Opens the file for reading. If the file doesn't exist, fopen() returns NULL.

Standard C Library Functions

Computer Programming: Skills & Concepts (CP) Files in C

System Software Experiment 1 Lecture 7

Files. Programs and data are stored on disk in structures called files Examples. a.out binary file lab1.c - text file term-paper.

Programming in C. Session 8. Seema Sirpal Delhi University Computer Centre

File (1A) Young Won Lim 11/25/16

Computer Programming Unit v

Day14 A. Young W. Lim Tue. Young W. Lim Day14 A Tue 1 / 15

2009 S2 COMP File Operations

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Day14 A. Young W. Lim Thr. Young W. Lim Day14 A Thr 1 / 14

File I/O. Arash Rafiey. November 7, 2017

SWEN-250 Personal SE. Introduction to C

CS240: Programming in C

Input/Output and the Operating Systems

File I/O. Preprocessor Macros

IO = Input & Output 2

Programming & Data Structure

File Handling. 21 July 2009 Programming and Data Structure 1

Continued from previous lecture

Systems Programming. 08. Standard I/O Library. Alexander Holupirek

Pointers and File Handling

CSE2301. Introduction. Streams and Files. File Access Random Numbers Testing and Debugging. In this part, we introduce

EM108 Software Development for Engineers

UNIX System Programming

C for Engineers and Scientists: An Interpretive Approach. Chapter 14: File Processing

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #47. File Handling

CSE 410: Systems Programming

CS240: Programming in C. Lecture 14: Errors

Basic I/O. COSC Software Tools. Streams. Standard I/O. Standard I/O. Formatted Output

The Design of C: A Rational Reconstruction: Part 2

PROGRAMMAZIONE I A.A. 2017/2018

8. Characters, Strings and Files

Naked C Lecture 6. File Operations and System Calls

C Basics And Concepts Input And Output

C programming basics T3-1 -

UNIT IV-2. The I/O library functions can be classified into two broad categories:

Advanced C Programming Topics

DATA STRUCTURES USING C

Princeton University Computer Science 217: Introduction to Programming Systems The Design of C

Programming. Functions and File I/O

UNIT-V CONSOLE I/O. This section examines in detail the console I/O functions.

Binghamton University. CS-211 Fall Input and Output. Streams and Stream IO

Should you know scanf and printf?

Contents. A Review of C language. Visual C Visual C++ 6.0

Standard I/O in C, Computer System and programming in C

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

System Programming. Standard Input/Output Library (Cont d)

CS201 Lecture 2 GDB, The C Library

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

Course organization. Course introduction ( Week 1)

CS 220: Introduction to Parallel Computing. Input/Output II. Lecture 8

Stream Model of I/O. Basic I/O in C

UNIX input and output

Text Output and Input; Redirection

What Is Operating System? Operating Systems, System Calls, and Buffered I/O. Academic Computers in 1983 and Operating System

7/21/ FILE INPUT / OUTPUT. Dong-Chul Kim BioMeCIS UTA

Goals of this Lecture

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

Data File and File Handling

Simple Output and Input. see chapter 7

Writing to and reading from files

HIGH LEVEL FILE PROCESSING

Input/Output: Advanced Concepts

EECS2031. Modifiers. Data Types. Lecture 2 Data types. signed (unsigned) int long int long long int int may be omitted sizeof()

CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS

CSI 402 Lecture 2 Working with Files (Text and Binary)

Chapter 10: File Input / Output

structs as arguments

fopen() fclose() fgetc() fputc() fread() fwrite()

Lecture 7: Files. opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments

ENG120. Misc. Topics

C Programming 1. File Access. Goutam Biswas. Lect 29

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

Introduction to file management

THE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY. ISA 563: Fundamentals of Systems Programming

How do we define pointers? Memory allocation. Syntax. Notes. Pointers to variables. Pointers to structures. Pointers to functions. Notes.

C for C++ Programmers

CSC 1107: Structured Programming

CSC 1107: Structured Programming

What did we talk about last time? Selection Loops Lab 3

Operating System Labs. Yuanbin Wu

Chapter 5, Standard I/O. Not UNIX... C standard (library) Why? UNIX programmed in C stdio is very UNIX based

Transcription:

Topic 8: I/O Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional) No C language primitives for I/O; all done via function calls. 1

Small Digression: C Library vs. System Calls System calls: functions provided with the Linux kernel, so that other programs may access system functionality. C Library includes access to these system calls. Also provides higher-level functions written for use in C programs. This topic: I/O using higher-level text-oriented C library functions defined in <stdio.h>. Also available: low-level I/O using system calls directly to read & write in binary format. 2

Example: Opening a Files To read from or write to a file (other than standard input/output): Need a "file pointer" type FILE *. To create a file pointer from a file name: FILE *fp = fopen(name, mode); File name: a string -- absolute or relative file name Mode: a string saying how the file will be used "r" = read "w" = write "a" = append In case of error: fopen returns NULL. 3

Reacting To Errors Goals for a useful program/function: legal input -> correct output illegal input -> informative output / return value Writing OS and other system programs: "garbage in, garbage out" is a very bad attitude! 4

Errors From C Library Functions Most C library functions deal with errors by doing two things: 1. returning a special value (like NULL for fopen) 2. setting the system variable errno errno is zero at start of program errno is not always reset when library functions are successful. to query errno, include <errno.h> User program should never change errno. 5

Error-Handling Steps Situation: You call a C library function like fopen, where an error is possible. 1. Check return value to see if error may have occurred (NULL for fopen). 2. If return value indicates possible error, check errno to verify error & find out what kind. 3. Report error to user and recover or end program. 6

Your Choices For Reporting Errors 1. ignore possibility of error bad idea except for quick throwaway programs! OR: check return value, and if it tells you there was an error: 2. write a general error message "could not open file", etc. OR: 3. compare errno with applicable error codes & write your own error message OR: 4. use library function to write an error message based on errno 7

Library Functions for Dealing With errno char *strerror(int errnum) Returns a string describing the error. Call this with errno as parameter and use the result in an error message. void perror(char *msg) Prints an error message to standard error, based on errno. If msg is not null, prints it first Libraries to include: <errno.h>: defines errno and macros for specific error values <string.h>: strerror & perror functions demo: IOtest1.c 8

My Advice Check results from library functions. If result indicates an error: report with perror. If simple way to recover, do it. Otherwise, abort program. What's important: realize that errors can occur error might be because of bug in program or bad input user needs to be told about nature of error aborting with informative error message is sometimes OK re-prompting in interactive program is OK crashing or behaving incorrectly because of error is not acceptable in a production program 9

Predefined File Pointers stdin, stdout, stderr: all declared in <stdio.h> stdin takes input from keyboard unless program is being run with standard input redirected stdout & stderr: writes to the screen unless program is being run with standard output/error redirected 10

Character-By-Character Input To read one character from a file: int getc(file *fp) Reads a single character from the file, not skipping white space. In case of error or end of file, returns EOF. How to tell the difference? errno To read one character from standard input: getchar() is equivalent to getc(stdin) demo: (IOtest2.c) 11

"ungetting" Characters int ungetc (int c, FILE *stream) "pushes" a character back onto the input stream Normally, c is the last character read from the file, but that's not required Most implementations only support one character pushback. Doesn't change the file, just "remembers" the pushed-back char. Why is this useful? Often you have to read one character after the end of something to realise you reached the end. Demo: IOtest3.c 12

Writing Characters int putc(int c, FILE *stream) Return value is: EOF if there was an error c if character was written To write one character to standard output: putchar(c) is equivalent to putc(c, stdout) 13

Writing Lines int fputs (char *s, FILE *stream) Writes s to the file. Does not write null or end of line at the end. If error: return value is EOF. int puts (char *s) Writes s to the standard output file with '\n' at the end. If error: return value is EOF. 14

Reading Lines char* fgets (char *s, int count, FILE *stream) Reads characters from the file into s until it reaches the end of the line or has read count-1 characters. Does not skip white space in the input. Always includes a null character at the end. If error or already at end of file: returns NULL (how can you tell the difference?) Otherwise, returns s. Demo: IOtest5.c 15

Reading Lines From Standard Input char* gets (char *s) Reads a line from the standard input. Dangerous don't use! (Why?) 16

Formatted Output We've already discussed printf: formatted output to standard output Formatted output to a file: int fprintf (FILE *stream, char *template,...) Return value: number of items printed -- or negative value if error 17

Formatted Input We've already discussed scanf: formatted input from standard input. Return value: number of values read EOF if hit end of file before any values read EOF if I/O error Input from a file: int fscanf (FILE *stream, const char *template,...) 18

"Output" To a String int sprintf (char *s, char *template,...) Formats output according to a template and copies to s instead of writing to a file. Always writes a null character at the end. Difficult to guarantee safety! Use with extreme care. Safe use of sprintf: IOtest6.c There are alternative versions of sprintf: one takes a maximum length as a parameter one allocates space for a result string on the heap 19

Closing Files When you're all done with a file: fclose(file); Two reasons to call fclose: 1. For an output file, makes sure all buffered output gets to the file. 2. Releases resources used by the file (on the heap). Result of fclose is an int: 0 if file was closed successfully EOF if an error occured 20