UNIT 12 UNIX I/O Redirection.

Slides:



Advertisements
Similar presentations
UNIT 15 File Processing.
Advertisements

CS1010 Programming Methodology
UNIX Chapter 12 Redirection and Piping Mr. Mohammad Smirat.
CS1010 Programming Methodology
CodeCrunch Getting Started.
Introduction to C Programming CE Lecture 15 Files and Program Parameters.
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.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Guide To UNIX Using Linux Third Edition
CS 117 Section 2 + KNET Computer accounts – ed to KNET students –Change password Homework 1 Lab Tutors –In lab for next 2 weeks –Will help you with.
Introduction to Unix – CS 21 Lecture 5. Lecture Overview Lab Review Useful commands that will illustrate today’s lecture Streams of input and output File.
WEEK 9 Class Activities Lecturer’s slides.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
CMPE13 Cyrus Bazeghi Chapter 18 I/O in C. CMPE Standard C Library I/O commands are not included as part of the C language. Instead, they are part.
Chapter Four UNIX File Processing. 2 Lesson A Extracting Information from Files.
Guide To UNIX Using Linux Fourth Edition
BIF703 stdin, stdout, stderr Redirection. stdin, stdout, stderr Recall the Unix philosophy “do one thing well”. Unix has over one thousand commands (utilities)
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
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.
LINUX programming 1. INDEX UNIT-III PPT SLIDES Srl. No. Module as per Session planner Lecture No. PPT Slide No. 1.Problem solving approaches in Unix,Using.
WEEK 4 Class Activities Lecturer’s slides.
UNIT 14 Functions with Pointer Parameters.
Chapter 18 I/O in C.
1. Introduction File Declaration and Initialization Creating and Opening File Closing File EOF Reading from and Writing into a File Extra : Random Access.
CS140: Intro to CS An Overview of Programming in C (part 3) by Erin Chambers.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Lecture 8a: File I/O BJ Furman 21MAR2011. Learning Objectives Explain what is meant by a data stream Explain the concept of a ‘file’ Open and close files.
WEEK 6 Class Activities Lecturer’s slides.
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.
Lecture 11: Files & Arrays B Burlingame 18 November 2015.
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:
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
UNIT 11 Random Numbers.
WEEK 8 Class Activities Lecturer’s slides.
 Simple UNIX commands  I/O techniques in C++  Solutions to Lab#0 problems  Solutions to Lab#1 problems 1.
UNIT 9 Arrays.
CS1010: Programming Methodology
CSC Programming for Science Lecture 18: More Data Files.
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.
1 Computer Programming Lecture 15 Text File I/O Assist. Prof Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
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.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets! Alphanumeric.
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
Chapter 7 Text Input/Output Objectives
CS1010 Programming Methodology
File Access (7.5) CSE 2031 Fall July 2018.
Chapter 18 I/O in C.
CS1010 Programming Methodology
Introduction to C CSE 2031 Fall /3/ :33 AM.
Plan for the Day: I/O (beyond scanf and printf)
File Input/Output.
Computer Programming Lecture 15 Text File I/O
Programming in C Input / Output.
Files I/O, Streams, I/O Redirection, Reading with fscanf
File I/O We are used to reading from and writing to the terminal:
Chapter Four UNIX File Processing.
Programming in C Input / Output.
Lecture 4 Redirecting standard I/O & Pipes
Module 12 Input and Output
Introduction to C EECS May 2019.
Chapter 11 Files chap8.
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
File I/O We are used to reading from and writing to the terminal:
Presentation transcript:

UNIT 12 UNIX I/O Redirection

Unit 12: UNIX I/O Redirection CS1010 (AY2014/5 Semester 1)Unit12 - 2© NUS Objective:  Learn how to use I/O redirection in UNIX to redirect input from a file and output to a file.

Unit 12: UNIX I/O Redirection CS1010 (AY2014/5 Semester 1)Unit12 - 3© NUS 1.Introduction 2.Input Redirection 3.Output Redirection 4.Combining Input and Output Redirection

1. Introduction CS1010 (AY2014/5 Semester 1)Unit12 - 4© NUS  Recall in Unit #3 Overview of C Programming, it is mentioned that the default standard input stream (stdin) is the keyboard, and the default standard output stream (stdout) is the monitor.  In UNIX, you may run a program that normally reads input data interactively to read the input data from a file instead.  Likewise, you may write the output of a program to a file instead of printing it on the screen.  This is known as input/output redirection.  Note that this is an operating system (UNIX) feature and not a C feature.

2. UNIX Input Redirection (1/3) CS1010 (AY2014/5 Semester 1)Unit12 - 5© NUS  Some programs read a lot of input data (eg: programs involving arrays), which makes it very inconvenient for users to key in the data interactively.  Instead, we may store the input data in a file, and let the program read the data from this file.  We may do it in 2 ways:  Read the file using file processing functions (eg: fopen(), fscanf(), fprintf()) – these will be covered next time  Redirect the input from the file instead of from stdin – we will do this for the moment

2. UNIX Input Redirection (2/3) CS1010 (AY2014/5 Semester 1)Unit12 - 6© NUS #include int main(void) { int num, sum = 0; printf("Enter integers, terminate with ctrl-d:\n"); while (scanf("%d", &num) == 1) { sum += num; } printf("Sum = %d\n", sum); return 0; } Unit12_Example.c  Running the program interactively: $ a.out Enter... With ctrl-d:  User enters ctrl-d here Sum = 33

2. UNIX Input Redirection (3/3) CS1010 (AY2014/5 Semester 1)Unit12 - 7© NUS  Using an editor (eg: vim), create a text file to contain the input data. Let’s call the file numbers. File numbers  Use the UNIX input redirection operator < to redirect input from the file numbers $ a.out < numbers Enter... With ctrl-d: Sum = 33  (This is how CodeCrunch runs your program. It redirects input from some file to feed your program.)

3. UNIX Output Redirection (1/2) CS1010 (AY2014/5 Semester 1)Unit12 - 8© NUS  Instead of printing your output to the default stdio (monitor), you may redirect the output to a file as well.  Use the UNIX output redirection operator >. $ a.out > outfile  User enters ctrl-d here

3. UNIX Output Redirection (2/2) CS1010 (AY2014/5 Semester 1)Unit12 - 9© NUS  The file outfile is created which captures all outputs of the program. $ cat outfile Enter integers, terminate with ctrl-d: Sum = 33  Output redirection > fails if the specified output file already exists  If you want to append the output of a program to an existing file, you may use >>

4. Combining Input and Output Redirection CS1010 (AY2014/5 Semester 1)Unit © NUS  You may combine both input and output redirection $ a.out outfile  Tip for lab exercises:  Using input redirection, you can download the given input files on the CS1010 and run your program on these files.  Using output redirection, you may now generate your own output file and compare it with the expected output file provided on the CS1010 website.  Use the UNIX diff command to compare two files. Example: diff file1 file2  If the two files compared are identical, no output will be generated by the diff command.

Summary CS1010 (AY2014/5 Semester 1)Unit © NUS In this unit, you have learned about Using UNIX input redirection < to redirect input from a file to a program Using UNIX output redirection > to redirect output of a program to a file

End of File CS1010 (AY2014/5 Semester 1)Unit © NUS