CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

Similar documents
Standard File Pointers

PROGRAMMAZIONE I A.A. 2017/2018

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

Input / Output Functions

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

CS240: Programming in C

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

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

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

File IO and command line input CSE 2451

Week Overview. Simple filter commands: head, tail, cut, sort, tr, wc grep utility stdin, stdout, stderr Redirection and piping /dev/null file

C Basics And Concepts Input And Output

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

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

Goals of this Lecture

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

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

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

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

Lecture 8: Structs & File I/O

Lecture 7: file I/O, more Unix

System Software Experiment 1 Lecture 7

File I/O, Project 1: List ADT. Bryce Boe 2013/07/02 CS24, Summer 2013 C

2009 S2 COMP File Operations

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

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

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

File I/O. Preprocessor Macros

Standard C Library Functions

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)

Common File System Commands

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

UNIX System Programming

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

Memory Layout, File I/O. Bryce Boe 2013/06/27 CS24, Summer 2013 C

CSE 410: Systems Programming

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

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

Process Management! Goals of this Lecture!

Data File and File Handling

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

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

ENG120. Misc. Topics

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

Naked C Lecture 6. File Operations and System Calls

Lecture 9: Potpourri: Call by reference vs call by value Enum / struct / union Advanced Unix

C File Processing: One-Page Summary

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

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

HIGH LEVEL FILE PROCESSING

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

C programming basics T3-1 -

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

Introduction to file management

Fundamentals of Programming. Lecture 15: C File Processing

Programming & Data Structure

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

CMPE-013/L. File I/O. File Processing. Gabriel Hugh Elkaim Winter File Processing. Files and Streams. Outline.

CS240: Programming in C

EM108 Software Development for Engineers

CS 261 Fall Mike Lam, Professor. Structs and I/O

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

File and Console I/O. CS449 Fall 2017

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

CE Lecture 11

Fundamentals of Programming & Procedural Programming

File and Console I/O. CS449 Spring 2016

Lecture 10: Potpourri: Enum / struct / union Advanced Unix #include function pointers

File Handling. 21 July 2009 Programming and Data Structure 1

C mini reference. 5 Binary numbers 12

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 12

Lecture 8. Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

Input/Output and the Operating Systems

LANGUAGE OF THE C. C: Part 6. Listing 1 1 #include <stdio.h> 2 3 int main(int argc, char *argv[]) PROGRAMMING

Physical Files and Logical Files. Opening Files. Chap 2. Fundamental File Processing Operations. File Structures. Physical file.

CSE 12 Spring 2016 Week One, Lecture Two

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

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

Operating Systems. Lecture 07. System Calls, Input/Output and Error Redirection, Inter-process Communication in Unix/Linux

Procedural Programming

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

Continued from previous lecture

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

Lecture 9: File Processing. Quazi Rahman

CSE 333 SECTION 3. POSIX I/O Functions

DATA STRUCTURES USING C

Introduction Variables Helper commands Control Flow Constructs Basic Plumbing. Bash Scripting. Alessandro Barenghi

CSE 12 Spring 2018 Week One, Lecture Two

Chapter 11 File Processing

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

File Processing. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

CSE 390a Lecture 2. Exploring Shell Commands, Streams, and Redirection

Summer June 15, 2010

Chapter 12. Files (reference: Deitel s chap 11) chap8

M.CS201 Programming language

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

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

Organization of a file

Ch 11. C File Processing (review)

Transcription:

1 Files 1.1 File functions Opening Files : The function fopen opens a file and returns a FILE pointer. FILE *fopen( const char * filename, const char * mode ); The allowed modes for fopen are as follows r Reading only. Start at beginning of file. r+ Reading and writing. Note that all writing is overwriting, not inserting. w Writing only. In other words, deletes contents of file (sets to 0 length). Cannot read previously written contents of file. w+ open for reading and writing (overwrite file) a Appending. Cannot read contents of file. a+ Reading and Appending. Can read, but all writes are done at the end of the file regardless of calls to fseek or similar. To open a file in a binary mode you must add a b to the end of the mode string; for example, rb (for the reading and writing modes, you can add the b either after the plus sign - r+b - or before - rb+ ) Closing a File : The function fclose returns zero on success, or EOF if there is an error in closing the file. This function actually, flushes any data still pending in the buffer to the file, closes the file, and releases any memory used for the file. The EOF is a constant defined in the header file stdio.h. int fclose( FILE *fp ); Writing a File : int fputc( int c, FILE *fp ); The function fputc writes the character value of the argument c to the output stream referenced by fp. It returns the written character written on success otherwise EOF if there is an error. You can use the following functions to write a null-terminated string to a stream: int fputs( const char *s, FILE *fp ); The function fputs writes the string s to the output stream referenced by fp. It returns a non-negative value on success, otherwise EOF is returned in case of any error. Reading a File : int fgetc( FILE * fp ); The fgetc function reads a character from the input file referenced by fp. The return value is the character read, or in case of any error it returns EOF. The following functions allow you to read a string from a stream: char *fgets( char *buf, int n, FILE *fp ); The functions fgets reads up to n 1 characters from the input stream referenced by fp. It copies the read string into the buffer buf, appending a null character to terminate the string. If this function encounters a newline character or the end of the file EOF before they have read the maximum number of characters, then it returns only the characters read up to that point including new line character. 1 March 23, 2014

Testing the End of a File : int feof(file *stream) returns a non-zero value when Endof-File indicator associated with the stream is set, else zero is returned. Flushing the Output Buffer of a Stream : int fflush(file *stream) The function fflush is used to flush an output stream or an update stream in which the most recent operation was not input. If strean is an input stream, the behavior is undefined. 1.2 Standard Streams In the header file stdio.h, stderr, stdin, and stdout are defined as macros. They are pointers to FILE types which correspond to the standard error, standard input, and standard output streams respectively. All file pointer arguments to the functions in Section 1.1 are considered streams. 2 Piping In Unix, one can use the standard output of a command as the input stream of another command. Such a feature is called piping. The symbol is the Unix pipe symbol used on the command line. Using a Pipe Recall that we can display the content of a file using the command cat: $ cat tobe.txt To be or not to be Instead of displaying the content of the file, the following commands send it through a pipe to the wc (word count) command. $ cat tobe.txt wc 3 10 42 $ cat tobe.txt wc -l 3 $ cat tobe.txt wc -w 10 Note that cat displays a file to stdout without pause. To view text page by page for a large file or several files, we can use pipe: $ cat tobe.txt EliotLoveSong.txt more In the above command, cat concatenate files tobe.txt and EliotLoveSong.txt, and through pipe, the stdout stream of cat becomes the input stream of the program more which is used for paging through text one screen at a time. Using sed to manipulate the text of a stream. 2 March 23, 2014

$cat tobe.txt $ cat tobe.txt sed s/to/not to/ not not $ cat tobe.txt In the above example, at the first shell promt, the content of the file tobe.txt was shown. At the second promt, the cat command to display the contents of the tobe.txt file, and send that display through a pipe to the sed command. The sed command took the input that it got through the pipe, changed the first occurrence of the word to on each line to not to, and then displayed its output to the screen. Note that, from another cat command, the original file was not changed. To change all occurrences of to to not to, we use the flag g as follows: $ cat tobe.txt sed s/to/not to/g not to be or not not to be not to be or not not to be Using egrep to search for strings in a file. To find the lines that contains the string question and display those lines to the standard output: $cat tobe.txt $ egrep question tobe.txt 3 Redirection The shell and many Unix commands take input from stdin, write output to stdout, and write error output to stderr where standard input is normally connected to the terminal keyboard and standard 3 March 23, 2014

output and error to the terminal screen. Redirection allows you to use a file in place of any of these three streams locations. The form of a command with standard input and output redirection is: $ command -[options] [arguments] < input file > output file In the following forms of redirection, file descriptor 0 is normally standard input (stdin), 1 is standard output (stdout), and 2 is standard error output (stderr). Command Description pgm > file Output of pgm is redirected to file pgm < file Program pgm reads its input from file. pgm file Output of pgm is appended to file. n > file Output from stream with descriptor n redirected to file. n file Output from stream with descriptor n appended to file. n >& m Merge output from stream n with stream m. n <& m Merge input from stream n with stream m. tag Standard input comes from here through next tag at start of line. Pipe: takes output from one program, or process, and sends it to another. Examples: $ wc tobe.c $ wc < tobe.c $ cat tobe.c wc $ ls -l >ls.txt $ program-name 2> error.log redirects the standard error stream to a file. Redirecting the standard error (stderr) and stdout to file $ cat tobe.txt blah >error.txt 2>&1 $ cat error.txt Redirecting the standard error to stdout $ ls -l blah 2>&1 ls: blah: No such file or directory $ ls -l blah wc -l ls: blah: No such file or directory 0 4 March 23, 2014

Discarding the output: Sometimes you will need to execute a command, but you don t want the output displayed to the screen. In such cases you can discard the output by redirecting it to the file /dev/null: $ command > /dev/null discards the standard output where command is the name of the command you want to execute and the file /dev/null is a special file that automatically discards all its input. For example, $ cat tobe.txt blah $ cat tobe.txt blah >/dev/null $ cat tobe.txt blah >/dev/null 2>&1 At the last shell prompt, both output of a command and its error output are discarded. $ cat tobe.txt blah 2> /dev/null Here, only its error output is discarded. 4 Exercise Write a C program that prints something to stdout and stderr. Get familiar with pipe. Redirect program out: stdout pipe to wc -l stdout only to file stderr only to file stderr to stdout then pipe to wc -l all output to file 5 March 23, 2014