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

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

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

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

Standard File Pointers

Input / Output Functions

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

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

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

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

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)

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

Standard C Library Functions

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

File IO and command line input CSE 2451

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

2009 S2 COMP File Operations

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

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.

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

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

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

Goals of this Lecture

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

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

Lecture 8: Structs & File I/O

Lecture 7: file I/O, more Unix

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

UNIX input and output

PROGRAMMAZIONE I A.A. 2017/2018

Input/Output: Advanced Concepts

25.2 Opening and Closing a File

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

Computer Programming Unit v

CS240: Programming in C

C Basics And Concepts Input And Output

HIGH LEVEL FILE PROCESSING

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

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

Basic and Practice in Programming Lab 10

scanf erroneous input Computer Programming: Skills & Concepts (CP) Characters Last lecture scanf error-checking our input This lecture

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

Input/Output and the Operating Systems

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

C Programming Language

Fundamentals of Programming. Lecture 10 Hamed Rasifard

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

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

M.CS201 Programming language

Process Management! Goals of this Lecture!

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

Lecture 9: File Processing. Quazi Rahman

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

Library Functions. General Questions

File I/O. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

File I/O. Preprocessor Macros

8. Characters, Strings and Files

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

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

IO = Input & Output 2

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

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

Princeton University. Computer Science 217: Introduction to Programming Systems. I/O Management

Slide Set 8. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Data File and File Handling

C programming basics T3-1 -

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

Welcome! COMP s1. Programming Fundamentals

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

Binghamton University. CS-220 Spring Includes & Streams

Princeton University Computer Science 217: Introduction to Programming Systems. I/O Management

CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS

DS: CS Computer Sc & Engg: IIT Kharagpur 1. File Access. Goutam Biswas. ect 29

UNIX Shell. The shell sits between you and the operating system, acting as a command interpreter

Princeton University Computer Science 217: Introduction to Programming Systems I/O Management

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

EM108 Software Development for Engineers

File Handling. 21 July 2009 Programming and Data Structure 1

LESSON 4. The DATA TYPE char

File I/O BJ Furman 23OCT2010

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

Programming Fundamentals

CSE 230 Intermediate Programming in C and C++ Input/Output and Operating System

Programming & Data Structure

Organization of a file

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

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

Unit 6 Files. putchar(ch); ch = getc (fp); //Reads single character from file and advances position to next character

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

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

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

The Design of C: A Rational Reconstruction: Part 2

Continued from previous lecture

C PROGRAMMING. Characters and Strings File Processing Exercise

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

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

CMPSC 311- Introduction to Systems Programming Module: Input/Output

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

Pointers cause EVERYBODY problems at some time or another. char x[10] or char y[8][10] or char z[9][9][9] etc.

EE458 - Embedded Systems Lecture 4 Embedded Devel.

Transcription:

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

Today s lecture Character oriented I/O (revision) Files and streams Opening and closing files CP1 26 slide 2 18th November, 2010

Idiom for character-oriented I/O char c; while ((c = getchar())!= EOF) { /* Code for processing the character c */ CP1 26 slide 3 18th November, 2010

File length char c; int length = 0; while ((c = getchar())!= EOF) { ++length; printf("file length is %d\n", length); Don t forget to initialise length, i.e. the length = 0 part. CP1 26 slide 4 18th November, 2010

Copying a file char c; while ((c = getchar())!= EOF) { putchar(c); Note that putchar(c) is the equivalent to printf("%c", c) CP1 26 slide 5 18th November, 2010

Copying a file, checking for errors char c; while ((c = getchar())!= EOF) { /* The manual says putchar returns the character written, or EOF on error (e.g. disk full) */ if ( putchar(c) == EOF ) { perror("error writing file"); exit(1); CP1 26 slide 6 18th November, 2010

Example: Count occurrences of uppercase letters int main(void) { int c, countu; countu = 0; while ((c = getchar())!= EOF) { if (isupper(c)) { countu += 1; printf("%d uppercase letters\n", countu); CP1 26 slide 7 18th November, 2010

The Unix I/O model An executing program has a standard input, a standard output, and a standard error. We ve been using these they re all usually the terminal. getchar(), putchar(), printf() etc. all use standard input/output. CP1 26 slide 8 18th November, 2010

Unix file redirection The Unix shell lets one specify the standard input, output and error for the program: Input from a file:./ftour < data50 Output to a file:./ftour > log Input and output redirection:./ftour < data50 > log Input and output from/to a program (piping): cat data50./ftour grep length CP1 26 slide 9 18th November, 2010

Streams In C we talk about input and output streams getchar() reads from the standard input stream putchar(ch) writes to the standard output stream You might think of a stream as a file but in practice, streams often end at a keyboard, a window or another program. It is more accurate to think of streams as connectors to files etc., which hide the tricky details. (You don t need to know whether your stream is a file, terminal, network connection etc.) CP1 26 slide 10 18th November, 2010

Standard Streams All C programs begin with three standard streams stdin is read by getchar() stdout is written to by putchar(c) stderr is a second output stream, used by error message functions (e.g. perror()). These streams are defined in stdio.h. CP1 26 slide 11 18th November, 2010

Using named streams All the standard I/O functions have a variant that has a named stream as a parameter fprintf(stdout, "Hello") printf("hello") putc(c, stdout) putchar(c) getc(stdin) getchar() Use the manual pages to find the variants! Same idea as sscanf, sprintf for strings. CP1 26 slide 12 18th November, 2010

Remember practical 2 void SkipWhiteSpace(void) { int ch = ReadChar(); while (ch == ch == \n ch == \t ) { ch = ReadChar(); UnReadChar(ch); CP1 26 slide 13 18th November, 2010

Using standard calls void SkipWhiteSpace(void) { int ch = getc(stdin); /* or getchar() */ while (ch == ch == \n ch == \t ) { ch = getc(stdin); /* or getchar() */ ungetc(ch, stdin); /* There is no ungetchar(ch) */ CP1 26 slide 14 18th November, 2010

Example: Replace iz by is int main(void) { int c, prev = 0; while ((c = getchar())!= EOF) { if (prev == i && c == z ) { putchar( s ); else { putchar(c); prev = c; CP1 26 slide 15 18th November, 2010

Using named streams int main(void) { int c, prev = 0; while ((c = getc(stdin))!= EOF) { if (prev == e && c == z ) { putc( s, stdout); else { putc(c, stdout); prev = c; CP1 26 slide 16 18th November, 2010

Streams have the type FILE *. E.g. Opening new streams FILE *stdin, *stdout, *stderr; FILE *wordlist; Streams do not always end in a file despite the name! CP1 26 slide 17 18th November, 2010

Opening files FILE *wordlist; wordlist = fopen("wordlist.txt", "r"); if (wordlist == NULL) { printf("can t find the word list\nbye!\n"); return EXIT_FAILURE; /* To be completed */ fclose(wordlist); CP1 26 slide 18 18th November, 2010

fopen() FILE *fopen(const char *path, const char *mode) Opens a stream for the file named path E.g. fopen("output.txt", "w"); E.g. fopen("/usr/include/stdio.h", "r"); The mode selects read or write access This prevents accidents Anyway, you can t write to a CD-Rom. fopen() returns NULL on failure CP1 26 slide 19 18th November, 2010

fopen() modes "r": Open text file for reading "w": Open text file for writing "a": Open text file for appending and several others... What happens if the file exists already? CP1 26 slide 20 18th November, 2010

Copying a File FILE *in, *out; in = fopen("wordlist.txt", "r"); out = fopen("copy.txt", "w"); while ((c = getc (in))!= EOF) { putc(c, out); fclose(in); fclose(out); CP1 26 slide 21 18th November, 2010

fclose() discards a stream fclose() It is good practice to close streams when they are no-longer needed, to avoid operating system limits. Exiting a program closes all streams. CP1 26 slide 22 18th November, 2010

perror(): reporting errors fopen() may return NULL for many reasons File not found Invalid path Permission denied Out of disk space Etc. perror() prints an error related to the last failed system call. CP1 26 slide 23 18th November, 2010

Using perror() FILE *wordlist; wordlist = fopen("silly.txt", "r"); if (wordlist == NULL) { perror("can t open word list"); return EXIT_FAILURE; :./prac3 Can t open word list: No such file or directory CP1 26 slide 24 18th November, 2010

Buffering (Most) streams are buffered: Text written to a stream may not appear immediately. fflush(file *stream) forces the pending text on a stream to be written. As does fclose(stream). fprintf(stream, "\n"); Streams connected to terminals are usually flushed after each newline character. CP1 26 slide 25 18th November, 2010

Summary: Streams Have the type FILE * Programs start with three streams stdin stdout stderr CP1 26 slide 26 18th November, 2010

Summary: New functions fopen() open a stream for a file getc() similar to getchar() putc() similar to putchar() fprintf() similar to printf() fscanf() similar to scanf() fclose() closes a stream fflush() flushes a buffer perror() reports an error in a system call CP1 26 slide 27 18th November, 2010